12.29

发布时间:2024年01月01日

#include <iostream>

using namespace std;
class Person{
    int *age;
    string &name;
public:
    Person(string name):age(new int),name(name){}
    Person(int age,string name):age(new int(age)),name(name){}
    ~Person(){
        delete age;
    }
    Person(const Person &other):age(new int (*(other.age))),name(other.name){}
    Person &operator=(const Person &other){
        this->name=other.name;
        *(this->age)=*(other.age);
        return *this;
    }
    void show(){
        cout<<"age="<<*age<<endl;
        cout<<"name="<<name<<endl;
    }
    void show1(){
        cout<<"ages="<<*age<<endl;
    }
   Person operator+(Person &other);
    friend Person operator+(Person &c1,Person &c2);
     friend Person operator*(Person &c1,Person &c2);
     Person operator*(Person &other);
     friend Person operator-(Person &c1,Person &c2);
     Person operator-(Person &other);
     friend Person operator/(Person &c1,Person &c2);
     Person operator/(Person &other);
     friend Person operator%(Person &c1,Person &c2);
     Person operator%(Person &other);
     bool operator==(Person &other);
     bool operator!=(Person &other);
     bool operator&&(Person &other);
     operator int();
     void operator()();
     friend Person operator++(Person &c1);
    friend Person operator--(Person &c1);
   friend Person operator++(Person &c2,int );
   friend Person operator--(Person &c2,int);


};

class Stu{
    double *score;
    Person p1;
public:
    Stu(double score,int age,string name):score(new double(score)),p1(age,name){}
    ~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();
        }


};
Person Person::operator+(Person &other){
    Person t(0,"zhangsan");
   *(t.age)=*(this->age)+(*other.age);

    return t;
}
Person operator+(Person &c1,Person &c2){
    Person t(0,"zhangsan");
   *(t.age)=*(c1.age)+(*(c2.age));

   return t;
}
Person operator*(Person &c1,Person &c2){
    Person t(0,"zhangsan");
   *(t.age)=*(c1.age)*(*(c2.age));

   return t;
}
Person Person::operator*(Person &other){
    Person t(0,"zhangsan");
    *(t.age)=*(this->age)*(*other.age);

    return t;
}
Person operator-(Person &c1,Person &c2){
    Person t(0,"zhangsan");
  *(t.age)=*(c1.age)-(*(c2.age));

    return t;
}
Person Person::operator-(Person &other){
    Person t(0,"zhangsan");
   *(t.age)=*(this->age)-(*other.age);

    return t;
}
Person operator/(Person &c1,Person &c2){
    Person t(0,"zhangsan");
    *(t.age)=*(c1.age)/(*(c2.age));

    return t;
}
Person Person::operator/(Person &other){
    Person t(0,"zhangsan");
    *(t.age)=*(this->age)/(*other.age);

    return t;
}
Person operator%(Person &c1,Person &c2){
    Person t(0,"zhangsan");
    *(t.age)=*(c1.age)%(*(c2.age));

    return t;
}
Person Person::operator%(Person &other){
    Person t(0,"zhangsan");
    *(t.age)=*(this->age)%(*other.age);

    return t;
}
bool Person::operator==(Person &other){
    if(this->age==other.age){
        if(this->name==other.name){
            return this->name==other.name;
        }
    }
}
bool Person::operator!=(Person &other){
    if(this->age!=other.age){
        return this->age!=other.age;
    }else{
        if(this->name!=other.name){
            return this->name!=other.name;
        }else{
            return this->name!=other.name;
        }
    }
}
bool Person::operator&&(Person &other){
    return (*(this->age)&&*(other.age));
}
void Person::operator()(){
    cout<<"hello"<<endl;
}


Person operator++(Person &c1){
    ++(c1.age);

    return c1;
}
Person operator--(Person &c1){
    --(c1.age);

    return c1;
}
Person operator++(Person &c2,int ){
  Person t(0,"zhangsan");
    t.age=c2.age++;

    return t;
}
Person operator--(Person &c2,int ){
   Person t(0,"zhangsan");
    t.age=c2.age--;

    return t;
}
int main()
{
    Person c1(1,"wangwu");
    Person c2(2,"lisi");
   Person c3=c1.operator*(c2);
   c3.show1();
   c3=c1.operator-(c2);
   c3.show1();
   c3=c1.operator/(c2);
   c3.show1();
    Stu s2(100,20,"zhangsan");
    s2.show();
    return 0;
}

文章来源:https://blog.csdn.net/Sujianrui/article/details/135327149
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。