In the following example, a class test is defined, and the <, +, + =, <,> and other symbols are reloaded:
# Include <iostream>
# Include <vector>
Using namespace STD;
Class test {
Public:
Int V;
/* Constructor */
Test (): V (0 ){}
Test (const Int & A): V (){}
Test (const Test & T1): V (t1.v ){}
/* The following heavy loads are less than signs <*/
// Compare the sizes of two objects
Bool operator <(const Test & T1) const {
Return (v <t1.v );
}
// Compare the object and INT size
Bool operator <(const Int & T1) const {
Return (v <t1 );
}
// Compare the int and object size of the youyuan Function
Friend inline bool operator <(const Int & A, const Test & T1 ){
Return (A <t1.v );
}
/* The following overload value = */
// Assign values between objects
Test & operator = (const Test & T1 ){
V = t1.v;
Return * this;
}
// Assign an int value to an object
Test & operator = (const Int & T1 ){
V = T1;
Return * this;
}
/* The following heavy load plus sign + */
// Add int to the object
Test operator + (const Int & ){
Test T1;
T1.v = V +;
Return T1;
}
// Add object
Test operator + (Test & T1 ){
Test T2;
T2.v = V + t1.v;
Return T2;
}
/* Add the equal sign + = */
// Add an object to an object
Test & operator + = (const Test & T1 ){
V + = t1.v;
Return * this;
}
// Add int to the object
Test & operator + = (const Int & ){
V + =;
Return * this;
}
/* The following heavy duty dual equal sign = */
// Object = Object
Bool operator = (const Test & T1) const {
Return (V = t1.v );
}
// Object = int
Bool operator = (const Int & T1) const {
Return (V = T1 );
}
/* The following overload input> output <*/
/* Youyuan function, output object */
Friend inline ostream & operator <(ostream & OS, Test & T1 ){
Cout <"class T (" <t1.v <")" <Endl;
Return OS;
}
/* Youyuan function, input object */
Friend inline istream & operator> (istream & is, Test & T1 ){
Cin> t1.v;
Return is;
}
};
Int main (){
Test T0, T1 (3 );
Test T2 (T1 );
Cout <t0 <t1 <t2;
Cin> T1;
T2 = T1;
T2 + = T1;
T1 + = 10;
Cout <t2;
If (t1 <t2) cout <"t1 <t2 ";
Else if (T1 = t2) cout <"T1 = t2 ";
Else/* T1> T2 */cout <"T1> t2 ";
Cout <Endl;
System ("pause ");
Return 0;
}