The following is an introduction to the C + + operator overloaded member functions and friend functions, the need for friends can come over the reference
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)//without the 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)//without the 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.
*****************************/