#include<bits/stdc++.h>
using namespace std;
class ppp{
public :
int a,b;
char *x;
ppp operator+(ppp &f);
ppp operator-(ppp &f);
ppp(const ppp &e)//拷贝构造函数
{
this->a=e.a;
this->b=e.b;
}
ppp(int c,int d)//构造函数
{
a=c;
b=d;
}
ppp(void)//构造函数
{
}
void show()
{
cout<<a<<" "<<b<<endl;
}
};
ppp ppp:: operator+(ppp &f)//运算符重载成员函数
{
ppp temp;
temp.a=this->a+f.a;
temp.b=this->b+f.b;
return temp;
}
ppp ppp ::operator-(ppp &f)//运算符重载成员函数
{
ppp temp;
temp.a=this->a-f.a;
temp.b=this->b-f.b;
return temp;
}
int main()
{
ppp a(5,5);
a.show();
ppp b(6,9);
b.show();
ppp c;
c=a-b;
c.show();
c=a+b;
c.show();
ppp d=a;
d.show();
}
和io流不同的是,别的运算符重载可以写为成员函数,也无需重载为类的友元函数!!!