C + + operator overloading (member function mode)

Source: Internet
Author: User

One, operator overloading

The operands of predefined operators in C + + can only be basic data types, and in fact, for many user-defined types, similar operations are required. What would happen if these existing operators in C + + were directly acting on user-defined type data? The compiler cannot give normal results, because we need operator overloading to give operators multiple meanings, so that the same operator acting on different types of data results in different types of behavior, enhancing the universality of the operators.



The essence of operator overloading is function overloading. In the implementation process, the specified operation expression is converted into a call to the operator function, the operand is converted to the argument of the operator function, and then according to the type of the argument to determine the need to call the compliance function, the process is completed during the compilation process.

The operator overloading rules are as follows:
Operators in ①, C + + are all overloaded except for a few, and can only overload existing operators in C + +.
The precedence and binding of operators after ② and overloading do not change.
③, operator overloading is the actual need for the new type of data, the original operator is modified appropriately. In general, the functionality of overloading should be similar to the original function, cannot change the number of operands of the original operator, and at least one Action object is a custom type.

Only five operators in C + + cannot be overloaded: the member operator ".", the pointer operator "*", the scope operator "::","sizeof", the conditional operator? : ".

There are two types of operator overloading, overloading the member functions of the class and overloading the friend function for the class.

The general syntax for operator overloading for member functions of a class is:

[CPP]View Plaincoy
1 operator  23     function body; 4

The general syntax for operator overloading for a friend function of a class is:

[CPP]View Plaincopy
1 operator  23 4    


Where the function type is the result type of the operation; operator is the keyword that defines the operator overload function; The operator is the overloaded operator name.
When an operator is overloaded as a member function of a class, the number of arguments to the function is less than the number of previous operations, and when overloaded with a friend function of a class, the number of arguments is the same as the number of original operands. The reason is that when you overload a member function for a class, if an object uses an overloaded member function, its own data can be accessed directly, it is not required to be passed in the parameter table, and the fewer operands are the object itself. While overloading is a friend function, the friend function operates on the data of an object, which must be done by the name of the object, so the parameters used are passed, and the number of operands is not changed.
The main advantage of operator overloading is that it allows you to change the operation of operators that are used inside the system to accommodate similar operations of user-defined types.

example of operator overloading program (member function mode)

[CPP]View Plaincopy
1 //operator Overloading: how member functions2#include <iostream>3 using namespacestd;4 5 classComplex//Plural class6 {7  Public:8Complex () {real = Imag =0;}9ComplexDoubleRDoublei)Ten     { OneReal =R; AImag =i; -     } -Complexoperator+ (ConstComplex &c); theComplexoperator- (ConstComplex &c); -Complexoperator* (ConstComplex &c); -Complexoperator/ (ConstComplex &c); -  +FriendvoidPrintConstComplex &c);//friend function -  + Private: A     DoubleReal//Real Department at     DoubleImag//Imaginary Part -  - }; -  -Inline Complex complex::operator+ (ConstComplex &c)//defined as inline function, code copy, high efficiency - { in     returnComplex (real + c.real, Imag +c.imag); - } to  +Inline Complex complex::operator- (ConstComplex &c) - { the     returnComplex (Real-c.real, imag-c.imag); * } $ Panax NotoginsengInline Complex complex::operator* (ConstComplex &c) - { the     returnComplex (Real * c.real-imag * c.imag, Real * c.real + imag *c.imag); + } A  theInline Complex complex::operator/ (ConstComplex &c) + { -     returnComplex (Real * c.real + imag * C. Imag)/(C.real * c.real + c.imag *c.imag), $(IMAG * c.real-real * c.imag)/(C.real * c.real + c.imag *c.imag)); $ } -  - voidPrintConstComplex &c) the { -     if(C.imag <0)Wuyicout<<c.real<<c.imag<<'I'<<Endl; the     Else -cout<<c.real<<'+'<<c.imag<<'I'<<Endl; Wu } -  About intMain () $ {     -Complex C1 (2.0,3.5), C2 (6.7,9.8), C3; -C3 = C1 +C2; -cout<<"C1 + C2 ="; APrint (C3);//A friend function is not a member function, only a normal function call, and cannot be called by an object of a class. +  theC3 = C1-C2; -cout<<"C1-C2 ="; $ Print (C3); the  theC3 = C1 *C2; thecout<<"C1 * C2 ="; the Print (C3); -  inC3 = C1/C2; thecout<<"C1/C2 ="; the Print (C3); About     return 0; the}

third, the program operation results

C + + operator overloading (member 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.