运算符重载

#include<iostream>
using namespace std;
class A{
  public:
      A(int a,int b){this->a=a;this->b=b;}
      int a,b;
      void display(){cout<<a<<" "<<b<<endl;}

    friend A operator+(A &A1,A &A2);//友元函数重载+
    A operator-(A &A2);             //成员函数重载-

   A operator++(int );            //成员函数重载后置++
   A operator--();                //成员函数重载前置++
   friend A operator++(A &A1);     //友元函数重载前置--
   friend A operator--(A &A1,int);  //友元函数重载后置--
};

      int main()
      {   A m(1,2);
          A k(3,2);
   cout<<"m(1,2)cout+||-k(3,2)"<<endl;
          (m+k).display();
          (m-k).display();

          cout<<endl;
    m.display();
    cout<<"m++"<<endl;
          (m++).display();
          (m++).display();
          cout<<endl;
     m.display();
    cout<<"--m"<<endl;
          (--m).display();
          (--m).display();
          cout<<endl;
    m.display();
    cout<<"++m"<<endl;
          (++m).display();
          (++m).display();
                 cout<<endl;
    m.display();
    cout<<"m--"<<endl;
    (m--).display();
    (m--).display();

          return 0;
      }

      A operator+(A &A1,A &A2)
       { A A3(A1.a+A2.a,A1.b+A2.b);
        return A3;}

    A A::operator-(A &A2)
    {
        A A3(0,0);
        A3.a=a-A2.a;
        A3.b=b-A2.b;
        return A3;   }

   A A::operator++(int Postposition)
   {
       int atemp=a;
       int btemp=b;
       a++;
       b++;
       return A(atemp,btemp);
   }
   A A::operator--()
   {
       a--;
       b--;
       return A(a,b);
   }
   A operator++(A &A1)
   {  A1.a++;
      A1.b++;
       return A(A1.a,A1.b);
   }
   A operator--(A &A1,int Postposition)
   {
    int atmp=A1.a;
    int btmp=A1.b;
    A1.a--;
    A1.b--;
    return A(atmp,btmp);
   }

“`这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值