运算符重载

 

 


#include <iostream>

using namespace std;

class Person
{
private:
    int age;
   int *p;
public:
    //无参构造
    Person():p(new int(89))
    {
        age = 18;

        cout<<"无参构造完成"<<endl;
    }
    //有参构造
    Person(int age,int num)
    {
        this->age = age;
          p=new int(num);
        cout<<"有参构造完成"<<endl;

    }
    //拷贝构造函数
    Person (Person &other)
    {
        this->age=other.age;
        p=new int(*(other.p));
             cout<<"拷贝构造完成"<<endl;

}

    //拷贝赋值函数
    Person &operator=(const Person &other)
    {
       if(this!=&other)
       {
        this->age = other.age;
        *p = *(other.p);
       }
            cout<<"拷贝赋值完成"<<endl;
      return *this;
    }




    //析构函数
    ~Person()
    {

        delete p;
        p = nullptr;
        cout<<"析构完成"<<endl;

    }




   friend Person operator +(Person &P3,Person &P6);
   friend Person operator -(Person &P3,Person &P6);
   friend Person operator *(Person &P3,Person &P6);
   friend Person operator /(Person &P3,Person &P6);
   friend Person operator %(Person &P3,Person &P6);

//<,<=,>,>=,==,!=成员函数版

    bool  operator < (Person &P6)
    {

        if  (this->age < P6.age)
                return this->age < P6.age;
        else
                return this->age >= P6.age;
   }


    bool  operator <= (Person &P6)
    {

        if  (this->age <= P6.age)
                return this->age <= P6.age;
        else
                return this->age > P6.age;
   }


    bool  operator > (Person &P6)
    {

        if  (this->age > P6.age)
                return this->age > P6.age;
        else
                return 0;
   }


    bool  operator >= (Person &P6)
    {

        if  (this->age >= P6.age)
                return this->age >= P6.age;
        else
                return 0;
   }


    bool  operator == (Person &P6)
    {

        if  (this->age == P6.age)
                return this->age == P6.age;
        else
                return 0;

   }

    bool  operator != (Person &P6)
    {

        if  (this->age != P6.age)
                return this->age != P6.age;
        else
                return 0;
   }

    bool  operator && (Person &P6)
    {

        if  (this->age && P6.age )
                return 1;
        else
                return 0;
   }

    bool  operator || (Person &P6)
    {

        if  (this->age || P6.age )
                return 1;
        else
                return 0;
   }

    //!,~,-,++,--成员函数版

//    Person operator =(Person &P6)
//    {
//        this->age =  P6.age;

//    }

    Person &operator +=(Person &P6)
    {
        this->age += P6.age;

    }


    Person &operator -=(Person &P6)
    {
        this->age -=P6.age;


    }

    Person &operator *=(Person &P6)
    {
        this->age *= P6.age;

    }

    Person &operator /=(Person &P6)
    {
        this->age /= P6.age;


    }

    Person &operator %=(Person &P6)
    {
        this->age %= P6.age;



    }


    Person  &operator !()
    {
        !this->age;


    }

    Person  &operator ~()

    {
         ~this->age;


    }

//    Person  &operator -()

//    {
//        -(this->age);


//    }

    //a++ 需要哑元
    Person  &operator ++(int)
    {  static Person temp;
        temp.age = this->age;
        (this->age)++;
       return temp;

    }
   //++a
    Person  &operator ++()
    {
        ++this->age;
        return *this;

    }

    //a-- 需要哑元
    Person  &operator --(int)
    {   static Person temp;
        temp.age = this->age;
        this->age--;
       return temp;

    }
   //--a
    Person  &operator --()
    {
        --this->age;
        return *this;

    }



    void show();
    void show1();
};



//Person fun(Person other)
//{

//}

void Person::show()
{

    cout<<"输出的结果:"<<age<<endl;

}

void Person::show1()
{

    cout<<"自增前:"<<age<<endl;

}


//+-*/用全局版的运算符重载

Person operator +(Person &P3,Person &P6)
{
     Person temp;

    temp.age = P3.age+P6.age;

    return temp;
}

Person operator -(Person &P3,Person &P6)
{
      Person temp;

    temp.age = P3.age-P6.age;

    return temp;
}


Person operator *(Person &P3,Person &P6)
{
     Person temp;

    temp.age = P3.age*P6.age;

    return temp;
}

Person operator /(Person &P3, Person &P6)
{
      Person temp;

    temp.age = P3.age/P6.age;

    return temp;
}




Person operator %(Person &P3, Person &P6)
{
     Person temp;

    temp.age = P3.age%P6.age;

    return temp;
}

int main()
{
    int num=90;

//    Person p1;
//    Person p2(14,num);
//    fun(p2);
//   Person p4;
//    p4 = p2;

    Person P3(20,num);
    Person P6(60,num);

    cout<<"P3:"<<20<<'\t'<<"P6:"<<60<<endl;

    Person p5;

    p5 = P3+P6;

    p5.show();
    p5 = P3-P6;
    p5.show();
    p5 = P3*P6;
    p5.show();
    p5 = P3/P6;
    p5.show();
    p5 = P3%P6;
    p5.show();


    bool res = P3 < P6;
    cout<<res<<endl;
    res = P3 <= P6;
    cout<<res<<endl;
    res = P3 > P6;
    cout<<res<<endl;
    res = P3 >=P6;
    cout<<res<<endl;
    res = P3 == P6;
    cout<<res<<endl;
    res = P3 != P6;
    cout<<res<<endl;
    res = P3 && P6;
    cout<<res<<endl;
    res = P3 || P6;
    cout<<res<<endl;

//    P3 = P6;
//    P3.show();
    P3 += P6;
    P3.show();
    P3 -= P6;
    P3.show();
    P3 *= P6;
    P3.show();
    P3 /= P6;
    P3.show();
    P3 %= P6;
    P3.show();


    (!P6).show();
    ~P6;
    P6.show();
//    -P6;
//    P6.show();
    P6.show1();
   P6++;
            P6.show();
    P6.show1();
   (++P6).show();
    P6.show1();
   (P6--).show();
    P6.show1();
   (--P6).show();


    return 0;





}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值