运算符重载

    xiaoxiao2021-12-14  28

    #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); }

    “`

    转载请注明原文地址: https://ju.6miu.com/read-969851.html

    最新回复(0)