Copy Code code as follows:
#include <iostream>
using namespace Std;
Class A
{
int x,y;
Public
A (int xx,int yy): X (xx), Y (yy) {}
A () {x=0;y=0;}
A operator+ (const A&B)//No const limit, you can also
{return A (X+B.X,Y+B.Y);}
A operator-()
{return A (-x,-y);}
void Show ()
{cout<< "x=" <<x<< "y=" <<Y<<ENDL;}
};
void Test_a ()
{
A A1 (2008,512), A2 (2013,420), A3;
A3=A1+A2; Call operator overloaded function: A1.oprator + (A2)
A3.show ();
A1=-A1; Call operator overloaded function: A1.operator-()
A1.show ();
}
/***********************
Execution results
x=4021 y=93
x=-2008 y=-512
**********************/
Class B
{
int x,y;
Public
B (int xx,int yy): X (xx), Y (yy) {}
B () {x=0;y=0;}
Friend B operator+ (const b&a,const b&b);
Friend B operator-(const b&a);
void Show ()
{cout<< "x=" <<x<< "y=" <<y<<endl;};
};
B operator+ (const b&a,const b&b)
{return B (A.X+B.X,A.Y+B.Y);}
B operator-(const b&a)
{return B (-A.X,-A.Y);}
/***************************
Class B
{
int x,y;
Public
B (int xx,int yy): X (xx), Y (yy) {}
B () {x=0;y=0;}
Friend B operator+ (const b&a,const b&b)
{return B (A.X+B.X,A.Y+B.Y);}
Friend B operator-(const b&a)
{return B (-A.X,-A.Y);}
void Show ()
{cout<< "x=" <<x<< "y=" <<y<<endl;};
}
********************************/
int main ()
{
B B1 (1991,1105), B2 (2013,62), B3;
B3=B1+B2; Call operator overloaded function: A1.oprator + (A2)
B3.show ();
B1=-B1; Call operator overloaded function: A1.operator + ()
B1.show ();
}
/****************************
Run Result:
x=4004 y=1167
x=-1991 y=-1105
Process returned 0 (0x0) execution time:0.021 s
Press any key to continue.
*****************************/
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class A
{
int x,y;
Public
A (int xx,int yy): X (xx), Y (yy) {}
A () {x=0;y=0;}
A operator+ (const A&B)//No const limit, you can also
{return A (X+B.X,Y+B.Y);}
A operator-()
{return A (-x,-y);}
void Show ()
{cout<< "x=" <<x<< "y=" <<Y<<ENDL;}
};
void Test_a ()
{
A A1 (2008,512), A2 (2013,420), A3;
A3=A1+A2; Call operator overloaded function: A1.oprator + (A2)
A3.show ();
A1=-A1; Call operator overloaded function: A1.operator-()
A1.show ();
}
/***********************
Execution results
x=4021 y=93
x=-2008 y=-512
**********************/
Class B
{
int x,y;
Public
B (int xx,int yy): X (xx), Y (yy) {}
B () {x=0;y=0;}
Friend B operator+ (const b&a,const b&b);
Friend B operator-(const b&a);
void Show ()
{cout<< "x=" <<x<< "y=" <<y<<endl;};
};
B operator+ (const b&a,const b&b)
{return B (A.X+B.X,A.Y+B.Y);}
B operator-(const b&a)
{return B (-A.X,-A.Y);}
/***************************
Class B
{
int x,y;
Public
B (int xx,int yy): X (xx), Y (yy) {}
B () {x=0;y=0;}
Friend B operator+ (const b&a,const b&b)
{return B (A.X+B.X,A.Y+B.Y);}
Friend B operator-(const b&a)
{return B (-A.X,-A.Y);}
void Show ()
{cout<< "x=" <<x<< "y=" <<y<<endl;};
}
********************************/
int main ()
{
B B1 (1991,1105), B2 (2013,62), B3;
B3=B1+B2; Call operator overloaded function: A1.oprator + (A2)
B3.show ();
B1=-B1; Call operator overloaded function: A1.operator + ()
B1.show ();
}
/****************************
Run Result:
x=4004 y=1167
x=-1991 y=-1105
Process returned 0 (0x0) execution time:0.021 s
Press any key to continue.
*****************************/