Source: Internet
Author: User

`#include <iostream>using namespacestd;intMain () {intA =2, B =3; floatc =2.1f, d =1.2f; cout<<"A + b ="<<a+b<<Endl; cout<<"C + d ="<<c+d<<Endl; return 0;}`

We see the operator "+" complete the addition of float and int Two types, which is the operator overloading. The operator overloads for these built-in types have been implemented, but what if the classes we write ourselves now implement similar addition operations?? For example, now there is a point class, to achieve the addition of two points, the result is to add the horizontal ordinate, this time we need to write an operator overloaded function.

`#include <iostream>using namespacestd;classpoint{Doublex; Doubley; Public:    Doubleget_x () {returnx; }    Doubleget_y () {returny; } Point (DoubleX =0.0,DoubleY =0.0): X (x), Y (y) {}; Pointoperator+(point P);};//overloaded operator "+"Point point::operator+(point P) {Doublex = This->x +p.x; Doubley = This->y +p.y;    Point tmp_p (x, y); returntmp_p;}intMain () {point P1 (1.2,3.1); Point P2 (1.1,3.2); Point P3= p1+P2; cout<<p3.get_x () <<" "<<p3.get_y () <<Endl; return 0;}`

Operator overloading is implemented in two ways, either through a "friend function" or a "class member function."

1. Format of the friend function overloaded operator:

`class class Name {    operator  operator (formal parameter list);}; // Out-of- class definition format: return type operator operator (parameter table) {    // function Body }`

`class class Name {public:    operator  operator (formal parameter list);}; // Out-of- class definition Format return type class name::operator  operator (formal parameter    list) {// function Body }`

To put it this way, it is still not enough to compare the differences between the two implementations, we write the "+" and "-" overloads of the point class in two ways. The code is as follows:

`#include <iostream>usingStd::endl;usingstd::cout;classpoint{Doublex; Doubley; Public:    Doubleget_x () {returnx; }    Doubleget_y () {returny; } Point (DoubleX =0.0,DoubleV R0.0): X (x), Y (y) {}; Friend Pointoperator-(Point p1,point p2); Pointoperator+(point P);};//overloaded Operator "-"Pointoperator-(Point p1,point p2) {Doublex = p1.get_x ()-p2.get_x (); Doubley = p1.get_y ()-p2.get_y ();    Point P3 (x, y); returnP3;}//overloaded operator "+"Point point::operator+(point P) {Doublex = This->x +p.x; Doubley = This->y +p.y;    Point tmp_p (x, y); returntmp_p;}intMain () {point P1 (1.2,3.2); Point P2 (1.1,3.1); Point P3= p1+P2; Point P4=operator-(P1,P2); cout<<p3.get_x () <<" "<<p3.get_y () <<Endl; cout<<p4.get_x () <<" "<<p4.get_y () <<Endl; return 0;}`
View Code

I don't know what you see here. When you use the friend function to overload the two-tuple operator "-", the formal parameter is two, and the formal parameter is only one when the class member function is used. Because the this pointer exists in the class member function, which is equivalent to a parameter, the class member implementation operator overloading requires fewer formal parameters than the original, such as: the use of class member functions to implement the unary operator "-", there is no need for parameters. It is for this reason that the operator overloads of the friend function implementations are limited, for example: [], (),->, and = Cannot use the friend function to implement overloading of operators.

In the actual development process, single-mesh operators recommend overloading as member functions, while binocular operators recommend overloading as friend functions. It is usually easier to load the lower binocular operator overload into a member function than the friend function, but sometimes the binocular operator must be overloaded to a member function, such as an assignment operator =. And if you need to modify the state inside the object, you can usually choose to modify it using the class member function.

Only existing C + + operators can be overloaded in 1.c++, and users are not allowed to define new operators themselves.

Most of the operators in 2.c++ can be overloaded, in addition to the member access Operators ., the scope operator ::, the length operator sizeof , and the conditional operator ?:.

3. Operator overloading cannot change the number of operands (operands) of an operator. For example: "+" is an operator that implements two operands, and is still a binocular operator after overloading.

4. Overloading cannot change the original precedence and the original binding of the operator.

5. Operator overloading cannot be all predefined basic data in C + +, and is intended to prevent users from modifying the nature of the operators used for basic type data.

Related Keywords:
Related Article

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.