|
#include <iostream>
using namespace std;
class A
{
int a,b,c ;
public:
A(int x=0,int y=0,int z=0)
{
a=x,b=y,c=z;
}
A operator+(A t)
{
a+=t.a;
b+=t.b;
c+=t.c;
return *this;
}
/*friend A operator-(A t1,A t2)
{
A temp;
temp.a=t1.a-t2.a;
temp.b=t1.b-t2.b;
temp.c=t1.c-t2.c;
return temp;
}*/
void operator+=(A t)
{
a+=t.a;
b+=t.b;
c+=t.c;
}
friend void operator-=(A& t1,A t2)
{
t1.a-=t2.a;
t1.b-=t2.b;
t1.c-=t2.c;
}
void print()
{
cout<<a<<'\t'<<b<<'\t'<<c<<'\n';
}
};
void main()
{
A a1(1,2,3),a2(4,5,6),a3,a4;
a3=a1+a2;
//a4=a1-a2;
a1+=a3;
a2-=a4;
a1.print();
a2.print();
a3.print();
a4.print();
}
我想知道加法运算重载时this指针到底是怎么样的?还有为什么第一个友元函数在编译时报错?。。。小女子谢了 |
|