C ++类

news/2024/7/5 1:37:41

定义一个Person类,私有成员int age,string &name,定义一个Stu类,包含私有成员double *score,写出两个类的构造函数、析构函数、拷贝构造和拷贝赋值函数,完成对Person的运算符重载(算术运算符、条件运算符、逻辑运算符、自增自减运算符、插入/提取运算符)

#include <iostream>

using namespace std;

class person
{
    int age;
    string &name;
public:
    person(string &a):age(18),name(a){}
    person(int a,string &b):age(a),name(b){}
    ~person(){}
    person(const person &other):age(other.age),name(other.name){}
    person &operator=(const person&other)
    {
        this->age=other.age;
        this->name=other.name;
        return *this;
    }
    void show()
    {
        cout<<"age:"<<age<<endl<<"name:"<<name<<endl;
    }
    person operator+(const person&other)
    {
        int age=this->age+other.age;
        static string name;
        name = this->name+other.name;
        return person(age,name);
    }
    bool operator==(person &other)
    {
        if((this->age==other.age)&&(this->name==other.name))
            return true;
        else
            return false;
    }
    bool operator>(person &other)
    {
        if(this->age>other.age)
            return true;
        else
            return false;
    }
    bool operator&&(person &other)
    {
        return (this->age&&other.age)||(this->name==other.name);
    }
    int getage(){return age;}
    friend ostream &operator<<(ostream &out,person&c1);
    friend istream &operator>>(istream &in,person &c1);
};
ostream &operator<<(ostream &out,person&c1)
{
    out<<"age:"<<c1.age<<endl<<"name:"<<c1.name<<endl;
    return out;
}
istream &operator>>(istream &in,person &c1)
{
    in>>c1.age>>c1.name;
    return in;
}
class stu
{
    double *score;
    person p1;
public:
    stu(string &a):score(new double(12.5)),p1(a){}
    stu(double a,string &b):score(new double(a)),p1(b){}
    ~stu(){delete score;}
    stu(const stu&other):score(new double(*(other.score))),p1(other.p1){}
    stu &operator=(const stu&other)
    {
        *(this->score)=*(other.score);
        this->p1=other.p1;
        return *this;
    }
    void show()
    {
        cout<<"score:"<<*score<<endl;
        p1.show();
    }
};
int main()
{
    string a="zhangsan";
    string b="lisi";
    string c="wangwu";
    stu a1(a);
    a1.show();
    cout<<endl;
    person p1(a);
    cout<<p1<<endl;
    person p2(b);
    cout<<p2<<endl;
    person p3(c);
    cout<<p3<<endl;
    person p4=(p1+p2);
    p4.show();
    cout<<endl;
    cout<<(p1==p2)<<endl;
    cout<<(p1>p2)<<endl;
    cout<<(p1&&p2)<<endl;

    return 0;
}


http://lihuaxi.xjx100.cn/news/1939813.html

相关文章

[数据结构]链栈的创建,入栈和出栈

栈是一种在栈顶压入和弹出的数据结构&#xff0c;所以只在一端进行操作.为了减小遍历开支&#xff0c;所以链栈一般在首元节点处进行插入&#xff08;入栈&#xff09;. #include <stdio.h> #include <stdlib.h>typedef struct Node {int data;struct Node* next;…

文件摆渡系统如何实现网络隔离后的数据交换、业务流转?

近年来全球网络安全威胁态势的加速严峻&#xff0c;使得企业对于网络安全有了前所未有的关注高度。即便没有行业性的强制要求&#xff0c;但在严峻的安全态势之下&#xff0c;企业的网络安全体系建设正从“以合规为导向”转变到“以风险为导向”&#xff0c;从原来的“保护安全…

MIT_线性代数笔记:第 24 讲 马尔可夫矩阵;傅里叶级数

目录 马尔可夫矩阵 Markov matrices傅里叶级数 Fourier series 本讲学习马尔可夫矩阵和傅里叶级数&#xff0c;两者是关于特征值和投影矩阵的应用。 马尔可夫矩阵 Markov matrices A [ 0.1 0.01 0.3 0.2 0.99 0.3 0.7 0 0.4 ] A \begin{bmatrix} 0.1 & 0.01 & 0.3 \\…

我们公司内应届生身上的6个共性问题

如题目&#xff0c;本文主要是根据我们公司内真实的应届生身上共同的问题&#xff0c;总结而来。 1. 一天会做很多工作&#xff1a;会跟很多人对接&#xff0c;会一会忙这个一会忙哪个 现象&#xff1a; 说实话&#xff0c;这种情况&#xff0c;我看着都替她着急。自己正在解…

c++关联容器详细介绍

文章目录 关联容器的简介1. std::set2. std::multiset3. std::map4. std::multimap5. std::unordered_set 和 std::unordered_map 关联容器的迭代器1. 迭代器基础2. 使用迭代器遍历关联容器3. 结合使用标准算法库 关联容器高级特性1. 自定义比较函数2. 容器的内存管理3. 性能分…

C++基础语法——数组、函数、指针和结构体

本专栏记录C学习过程包括C基础以及数据结构和算法&#xff0c;其中第一部分计划时间一个月&#xff0c;主要跟着黑马视频教程&#xff0c;学习路线如下&#xff0c;不定时更新&#xff0c;欢迎关注。 当前章节处于&#xff1a; >第1阶段-C基础入门 ---------第2阶段实战-通讯…

亚信安慧AntDB数据库:企业核心业务系统数据库升级改造的可靠之选

在近期召开的“2023年国有企业应用场景发布会”上&#xff0c;亚信安慧公司的核心数据库产品AntDB闪耀登场&#xff0c;技术总监北陌先生针对企业核心业务系统数据库升级改造的关键议题发表了深度分享。他从研发、工程实施和运维管理三个维度细致剖析了当前企业在进行数据库升级…

【自学笔记】01Java基础-02java基础语法-02变量、对象与类

1 变量、对象、类 变量&#xff1a; 变量是在编程中用来存储数据的占位符&#xff0c;它具有名称和值。在程序执行过程中&#xff0c;变量的值可以改变。根据作用域和生命周期的不同&#xff0c;变量可分为全局变量、局部变量、类变量&#xff08;静态变量&#xff09;、实例变…