C ++类

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

#include 

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+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:"<>(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<p2)<

C ++类_第1张图片C ++类_第2张图片

你可能感兴趣的:(c++)