Example of C ++ heavy-load Operator

Source: Internet
Author: User
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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.