Methods of C + + operator overloading detailed parsing of _c languages

Source: Internet
Author: User

Operator overloading is essentially an overload of a function

The functions of overloaded operators are generally formatted as follows:

function type operator operator name (parameter list column)

{Overloaded handling of operators}

For example, to use "+" for the addition of complex (plural), the prototype of a function can be this:

Copy Code code as follows:

Complex operator + (Complex & C1,complex &c2);

Where operator is a keyword, the operator name is the predefined operator that C + + provides to the user, when it is specifically used to define the functions of the overloaded operator.

Note: The function name is made up of operator and operators.

The above operator+ is the function name, meaning "to operator + overload." With this in mind, it can be found that there is no difference in form between such functions and other functions.

Two parameters are references to complex class objects that require arguments to be complex class objects.

After the overloaded operator is defined, it can be said that the function operator+ overloaded the operator +.

In the execution of the expression c1+c2 of the plural addition (assuming that the C1+C2 has been defined as complex), the system invokes the operator+ function, combining the C1+C2 as the actual parameter with the formal parameter.

To illustrate that after overloading an operator, executing an expression is the process of calling a function, and you can add two integers to the image called the following function:

Copy Code code as follows:

int operator + (int a,int b)
{
return (A+B);
}

If there is an expression 5+8, this function is called, and 5 and 8 are the parameters of the calling function, and the return value of the function is 13, which is to understand the operator through the method of the function.

===================== Sample Code 1.1==================================

Copy Code code as follows:

#include <iostream>
using namespace Std;
Class Complex
{
Public
Complex ()
{
real=0;
imag=0;
}
Complex (double r,double i)
{
Real=r;
Imag=i;
}
Complex operator + (Complex &c2);//the "+" function of the declaring operator
void display ();
Private
Double Real;
Double imag;
};
Complex complex::operator+ (Complex &c2)
{
Complex C;
C.real=real+c2.real;
C.imag=imag+c2.imag;
return C;
}
void Complex::d isplay ()
{
cout<< "(" <<real<< "," <<imag<< "i)" <<endl;
}
int main ()
{
Complex C1 (3,4), C2 (5,-10), C3;
C3=C1+C2;
cout<< "c1=";
C1.display ();
cout<< "c2=";
C2.display ();
cout<< "c3=";
C3.display ();
return 0;
}

Analysis:

In the main function, "C3=C1+C2;" After the operator + overload is a member function of the class, the C + + compilation system interprets the expression C1+C2 in the program as:

c1.operator+ (C2)//Where C1+C2 is an object of the complex class

That is, the operator overloaded function operator+ (Complex & C2) called C1 with C2 as an argument is evaluated and the sum of two complex numbers is obtained. The "operator+" above is a function name, which is a member function of the class complex.

In practice, the declarations of classes and the use of classes are often separate. If the operator +,-,*,/are overloaded when declaring the complex class, then the user using the class can program without considering how the function is implemented,

It is very convenient to use +,-,*,/to carry on the complex number operation directly.

The above operator overloaded function operator + can also be rewritten a bit more concise:

Copy Code code as follows:

Complex complex::operator+ (Complex &c2)
{
Return Complex (C2.REAL+REAL,C2.IMAG+IMAG);
}

The complex (C2.REAL+REAL,C2.IMAG+IMAG) in the return statement is to create a temporary object that has no object name and is an unnamed object.

In the process of establishing a temporary object, the constructor is called. The return statement returns the temporary object as a function value.

So, after we overload the + operator, can we add a constant to a complex number? Like what:

Copy Code code as follows:

C3=3+C2; Error, does not match the formal parameter type

This is not workable, because when we define the operator + function, the formal parameter is two complex objects, that is, the arguments and the parameter matching can call the function.

Should be written in the form of an object, such as:

Copy Code code as follows:

C3=complex (3,0) +c2; Correct, types are objects

It also needs to be explained that after the operator is overloaded, its original function remains, without loss or change.

For example, after an operator + is overloaded, it can still be used to Int,float,double,char type data, while adding functionality to the complex number addition.

The compilation system depends on the context of the expression, that is, the data type based on the sides of the operator, if the monocular operator is one side.

For example, for 3+5, an integer addition is performed, and for 3.4+5.45, a double number addition is performed, and complex addition is performed for the two plural classes.

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.