Operator overloading is a member function __ function

Source: Internet
Author: User

Operator overloading, which is essentially a function overload, is overloaded as a member function, and it is free to access the data members of this class, and is always used to access overloaded operators through an object of the class. In the case of a binocular operator, the left-hand operand is the data of the object itself, and as indicated by the this pointer, no arguments are required. The two cases are described below.
for binocular operator B, if you want to overload a member function of a class to enable it to implement the expression Oprd1 b oprd2, where Oprd1 is an object of Class A, you should overload B with a member function of Class A, which has only one formal parameter, and the type of the parameter is the type that oprd2 belongs to. After overloading, an expression is equivalent to a function call Orpd.operator B (OPRD2).
for the predecessor single operator U, such as "-" (minus), if you want to overload the member function of a class to implement the expression U OPRD, where OPRD is an object of Class A, you should overload the member function of Class A, the function has no formal parameters, after overloading, the expression u OPRD corresponds to a function call Oprd.operator U ().
Take a second look at the back operator "+ +" and "–" If you want to overload a member function of a class to implement an expression oprd++ or oprd–, where OPRD is an object of Class A, the operator should be overloaded as a member function of Class A, when the function takes an integer (int) parameter. After overloading, an expression OPRD is equivalent to calling oprd.oprator++ (0) and oprd.oprator– (0). The int type parameter here does not have any effect in the operation, except that it differs from the predecessor.
Complex number class plus subtraction operation overload as member function form

 #include <iostream> #include <cassert> using namespace std; Class Complex {Public:complex (double r=0.0,double i=0.0): Real (R), Imag (i) {} complex operator+ (const complex &
C2) const;//operator + overloaded member function complex operator-(const complex &AMP;C2) const;//operator-overloaded member function void display () const;
    Private:double Real;
Double imag;
}; 
Complex complex::operator+ (const complex &AMP;C2) const {return complex (REAL+C2.REAL,IMAG+C2.IMAG);//Create a temporary nameless object as the returned value } complex complex::operator-(const complex &AMP;C2) const {return complex (REAL-C2.REAL,IMAG-C2.IMAG);} void Complex ::d isplay () const {cout<< "(" <<real<< "," <<imag<< ")" <<endl;} int main () {Comp
    Lex C1 (5,4), C2 (2,10), C3; cout<< "c1=";
    C1.display (); cout<< "c2=";
    C2.display (); c3=c1-c2;//uses overloaded operators to complete complex subtraction cout<< "c3=c1-c2=";
    C3.display ();
    C3=C1+C2; cout<< "c3=c1+c2=";
    C3.display ();
return 0;

 Overload the single eye operator "+ +" as a member function form. #include <iOstream> #include <cassert> using namespace std;
        Class Clock {public:clock (int hour=0,int minute=0,int second=0);
        void Showtime () const;
        clock& operator++ ();
    Clock operator++ (int);
Private:int Hour,minute,second;
}; Clock::clock (int hour,int minute,int second) {if (0<=hour&&hour<24&&0<=minute&&
        MINUTE&LT;60&AMP;&AMP;0&LT;=SECOND&AMP;&AMP;SECOND&LT;60) {this->hour=hour;
        this->minute=minute;
    this->second=second; else {cout<< "Time error!"
    <<endl;
} void Clock::showtime () const {cout<

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.