The following is an introduction to the C ++ operator's heavy-duty member functions and youyuan functions. If you need them, please refer to them.Copy codeThe Code is as follows:
# Include <iostream>
Using namespace std;
Class
{
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 is added. 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 overload function: a1.oprator + (a2)
A3.show ();
A1 =-a1; // call the operator overload function: a1.operator -()
A1.show ();
}
/***********************
Execution result
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 & );
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 &)
{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 &)
{Return B (-a. x,-a. y );}
Void show ()
{Cout <"x =" <x <"y =" <y <endl ;};
}
********************************/
Int main ()
{
B B1 (), B2 (), B3;
B3 = B1 + B2; // call operator overload function: a1.oprator + (a2)
B3.show ();
B1 =-B1; // call the operator overload function: a1.operator + ()
B1.show ();
}
/****************************
Running result:
X = 4004 y = 1167
X =-1991 y =-1105
Process returned 0 (0x0) execution time: 0.021 s
Press any key to continue.
*****************************/
Copy codeThe Code is as follows:
# Include <iostream>
Using namespace std;
Class
{
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 is added. 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 overload function: a1.oprator + (a2)
A3.show ();
A1 =-a1; // call the operator overload function: a1.operator -()
A1.show ();
}
/***********************
Execution result
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 & );
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 &)
{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 &)
{Return B (-a. x,-a. y );}
Void show ()
{Cout <"x =" <x <"y =" <y <endl ;};
}
********************************/
Int main ()
{
B B1 (), B2 (), B3;
B3 = B1 + B2; // call operator overload function: a1.oprator + (a2)
B3.show ();
B1 =-B1; // call the operator overload function: a1.operator + ()
B1.show ();
}
/****************************
Running result:
X = 4004 y = 1167
X =-1991 y =-1105
Process returned 0 (0x0) execution time: 0.021 s
Press any key to continue.
*****************************/