c++ 运算符重载实例

今日思维导图

 

c++运算符重载 实例

#include <iostream>

using namespace std;

class Person
{
private:
    int age;
    int height;
public:
    //无参构造
    Person():height(190)
    {
        age=18;
    }
    //有参构造
    Person(int age,int height)
    {
        this->age=age;
        this->height=height;
    }
    //拷贝构造函数
    Person(Person &other)
    {
        age=other.age;
        height=other.height;
        cout<<"调用了拷贝构造函数1"<<endl;
    }
    //拷贝赋值函数
   /*Person &operator=(Person &other)
    {
            age=other.age;
            height=other.height;
        cout<<"调用了拷贝赋值函数2"<<endl;
        return  *this;
     }*/
    //析构函数
    ~Person()
    {
        cout<<"--调用了析构函数--ending"<<endl;
    }
   void show()
   {
       cout<<"age="<<age<<endl;
       cout<<"height="<<height<<endl;
   }


   //算数运算符重载
   Person operator+(Person &p2)
   {
       Person temp;
       temp.age=this->age+p2.age;
       temp.height=this->height+p2.height;
       return temp;
   }
   Person operator-(Person &p2)
   {
       Person temp;
       temp.age=this->age-p2.age;
      temp.height=this->height-p2.height;
       return temp;
   }
   Person operator*(Person &p2)
   {
       Person temp;
       temp.age=this->age*p2.age;
       temp.height=this->height*p2.height;
       return temp;
   }
   Person operator/(Person &p2)
   {
        Person temp;
       temp.age=this->age/p2.age;
       temp.height=this->height/p2.height;
       return temp;
   }
   Person operator%(Person &p2)
   {
       Person temp;
       temp.age=this->age%p2.age;
       temp.height=this->height%p2.height;
       return temp;
   }

   // 赋值运算符
   Person &operator+=(Person &p2)
   {
       this->age+=p2.age;
       this->height+=p2.height;
       return *this;
   }
   Person &operator-=(Person &p2)
   {
       this->age-=p2.age;
       this->height-=p2.height;
       return *this;
   }
   Person &operator*=(Person &p2)
   {
       this->age*=p2.age;
       this->height*=p2.height;
       return *this;
   }
   Person &operator/=(Person &p2)
   {
       this->age/=p2.age;
       this->height/=p2.height;
       return *this;
   }
   Person &operator%=(Person &p2)
   {
       this->age%=p2.age;
       this->height%=p2.height;
       return *this;
   }

   //逻辑与与逻辑或运算符
   bool operator&&(Person &p2)
   {
       return (this->age&&p2.age)&&(this->height&&p2.height);
   }
   bool operator||(Person &p2)
   {
       return (this->age||p2.age)||(this->height||p2.height);
   }

   //关系运算符
   bool operator==(Person &p2)
   {
       return (this->age==p2.age)&&(this->height==p2.height);
   }
   bool operator!=(Person &p2)
   {
       return (this->age!=p2.age)||(this->height!=p2.height);
   }

   bool operator>=(Person &p2)
   {
       return (this->age>=p2.age)&&(this->height>=p2.height);
   }
   bool operator<=(Person &p2)
   {
       return (this->age<=p2.age)&&(this->height<=p2.height);
   }
   bool operator>(Person &p2)
   {
       return (this->age>p2.age)&&(this->height>p2.height);
   }
   bool operator<(Person &p2)
   {
       return (this->age<p2.age)&&(this->height<p2.height);
   }

   //单目运算符
   bool operator!()
   {
       return !(this->age||this->height);
   }
   Person operator~()
   {
       Person temp;
       temp.age=~this->age;
       temp.height=~this->height;
       return temp;
   }
   Person &operator++()//前缀自增
   {
       ++this->age;
       ++this->height;
       return *this;
   }
   Person &operator++(int)//后缀自增
   {
       static Person temp;
       temp=*this;
       this->age++;
       this->height++;
       return temp;
   }
   Person &operator--()//前缀自增
   {
       --this->age;
       --this->height;
       return *this;
   }
   Person &operator--(int)//后缀自增
   {
       Person temp;
       temp=*this;
       this->age--;
       this->height--;
       return temp;
   }
   Person operator-()//负号
   {
       static Person temp;
       temp.age=-this->age;
       temp.height=-this->height;
       return temp;
   }

};

int main()
{
    //Person p1;
    //无显示 会显示调用析构函数
    //Person p2=p1;
    //会显示调用拷贝构造函数
    //Person p3;
    //p3=p1;
    //会显示拷贝赋值函数
    cout<<"***********"<<endl;
    Person p1;
    p1.show();
    Person p2;
    p2.show();
    Person p3;
    p3=p1+p2;
    p3.show();
    cout<<(p1&&p2)<<endl;
    cout<<(!p1)<<endl;
    Person p4=p1++;
    p4.show();
    (-p1).show();
    (~p1).show();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值