C + + operator overloading (friend function mode)

Source: Internet
Author: User

We know that operator overloading in C + + has two forms: the ① overload is a member function of the class (see C + + operator overloading (member function mode)), and ② overloads the friend function for the class.

When you overload a friend function, there is no implied parameter of the this pointer. Thus, the pair binocular operator, the friend function has 2 parameters, the monocular operator, the friend function has a parameter. However, some operators cannot be overloaded as friend functions, which are: =, (), [], and->.

The overload function for an operator overloaded with a friend function is defined in the following format:

[CPP]View Plaincopy example of a program

[CPP]View Plaincopy
1 //operator overloading: friend function mode2#include <iostream.h>3 4 classComplex//Plural class5 {6  Public:7Complex () {real = Imag =0;}8ComplexDoubleRDoublei)9     {TenReal =R; OneImag =i; A     } -Friend Complexoperator+ (ConstComplex &AMP;C1,ConstComplex &AMP;C2);//compared to the member function, the friend function is preceded by a friend, one of the formal parameters, and the class domain is removed. -Friend Complexoperator- (ConstComplex &AMP;C1,ConstComplex &AMP;C2);//There are implicit parameters in the way of member functions, and no hidden parameters for friend functions theFriend Complexoperator* (ConstComplex &AMP;C1,ConstComplex &C2); -Friend Complexoperator/ (ConstComplex &AMP;C1,ConstComplex &C2); -  -FriendvoidPrintConstComplex &c);//friend function +  - Private: +     DoubleReal//Real Department A     DoubleImag//Imaginary Part at  - }; -  -Complexoperator+ (ConstComplex &AMP;C1,ConstComplex &C2) - { -     returnComplex (C1.real + c2.real, C1.imag +c2.imag); in } -  toComplexoperator- (ConstComplex &AMP;C1,ConstComplex &C2) + { -     returnComplex (C1.real-c2.real, C1.imag-c2.imag); the } *  $Complexoperator* (ConstComplex &AMP;C1,ConstComplex &C2)Panax Notoginseng { -     returnComplex (C1.real * C2.real-c1.imag * c2.imag, C1.real * c2.real + c1.imag *c2.imag); the } +  AComplexoperator/ (ConstComplex &AMP;C1,ConstComplex &C2) the { +     returnComplex ((C1.real * c2.real + c1.imag * c2. Imag)/(C2.real * c2.real + c2.imag *c2.imag), -(C1.IMAG * c2.real-c1.real * c2.imag)/(C2.real * c2.real + c2.imag *c2.imag)); $ } $  - voidPrintConstComplex &c) - { the     if(C.imag <0) -cout<<c.real<<c.imag<<'I'<<Endl;Wuyi     Else thecout<<c.real<<'+'<<c.imag<<'I'<<Endl; - } Wu  - intMain () About {     $Complex C1 (2.0,3.5), C2 (6.7,9.8), C3; -C3 = C1 +C2; -cout<<"C1 + C2 ="; -Print (C3);//A friend function is not a member function, only a normal function call, and cannot be called by an object of a class. A  +C3 = C1-C2; thecout<<"C1-C2 ="; - Print (C3); $  theC3 = C1 *C2; thecout<<"C1 * C2 ="; the Print (C3); the  -C3 = C1/C2; incout<<"C1/C2 ="; the Print (C3); the     return 0; About}

Second, the program operation results


From the running results we can see, whether through the member function or the use of friend function, its implementation of the functions are the same, are overloaded operators, expand its function, so that it can be applied to the user-defined type of the calculation.

Comparison of two overloaded modes (member function mode and friend function mode)

In general, the monocular operator is best to be overloaded as a member, the binocular operator is best to be overloaded as a friend function, the binocular operator overload for the friend function as a member function is more convenient for this, however, some binocular operators or overloads for the member function as well, for example, the assignment operator. Because, if it is overloaded as a friend function, there will be places that are inconsistent with the assignment semantics.

C + + operator overloading (friend function mode)

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.