Operator Overloading in C ++

Source: Internet
Author: User

Operator Overloading in C ++

1. What is Operator overload?

Operator overloading can be divided into two parts: "operator" and "overload ". When it comes to overloading, it should be no stranger. This is a kind of polymorphism during compilation. In fact, overloading can be divided into function overloading and operator overloading. The difference between operator overloading and function Overloading is that operator overloading must be an operator. Let's take a look at the so-called Operator Overloading:

#include <iostream>using namespace std;int main(){    int a = 2 , b = 3;    float c = 2.1f , d = 1.2f;    cout<<"a + b = "<<a+b<<endl;    cout<<"c + d = "<<c+d<<endl;    return 0;}

We can see that the operator "+" is complete.FloatAndIntTwo types of addition calculation, which is the heavy load of operators. These built-in types of Operator Overloading have been implemented, but what if we want to implement a similar addition operation for the classes we have already written ?? For example, now there is such a point class. To add two vertices, the result is that the horizontal and vertical coordinates must be added. At this time, we need to write an operator to reload the function.

# Include <iostream> using namespace std; class point {double x; double y; public: double get_x () {return x;} double get_y () {return y ;} point (double X = 0.0, double Y = 0.0): x (X), y (Y) {}; point operator + (point p );}; // overload operator "+" point: operator + (point p) {double x = this-> x + p. x; double y = this-> y + p. y; point tmp_p (x, y); return tmp_p;} int main () {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 ;}

  

2. Two Methods for implementing Operator Overloading

There are two methods to implement the operator overload, namely through the "youyuan function" or "class member function ".

1. Format of uyuanyuan function overload operators:

Class name {friend return type operator (form parameter table) ;}; // out-of-class Definition Format: return type operator (parameter table) {// function body}

  

2. Format of implementing Operator Overloading for class member functions:

Class name {public: return type operator (form parameter table) ;}; // format of out-of-class definition return type class Name: operator (form parameter table) {// function body}

In this case, it is not enough to compare the differences between the two implementation methods. We use two implementation methods to write the "+" and "-" overloading of the point class respectively. The Code is as follows:

# Include <iostream> using std: endl; using std: cout; class point {double x; double y; public: double get_x () {return x ;} double get_y () {return y;} point (double X = 0.0, double Y = 0.0): x (X), y (Y ){}; friend point operator-(point p1, point p2); point operator + (point p) ;}; // overload operator "-" point operator-(point p1, point p2) {double x = p1.get _ x ()-p2.get _ x (); double y = p1.get _ y ()-p2.get _ y (); point p3 (x, y ); return p3;} // The overload operator "+" point: operator + (point p) {double x = this-> x + p. x; double y = this-> y + p. y; point tmp_p (x, y); return tmp_p;} int main () {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 ;}

  

I don't know if we can see it here. When we use the friend functions to overload the binary operator "-", the form parameters are two. When using the class member functions, the form parameters have only one. In this case, because this pointer exists in the class member function, this is equivalent to a parameter, so the class member implementation operator overload requires one fewer formal parameter than the original one, such: the class member function is used to implement the unary operator "-", so no parameter is required. For this reason, there are restrictions on the operator overloading implemented by youyuan functions, such as [], (),-> and =. You cannot use youyuan functions to implement the operator overloading.

In actual development, we recommend that you reload the single-object operator as a member function, while the binary operator is recommended to be a friend function. Generally, the binary operator overload is more convenient than the member function overload, but sometimes the binary operator must be overloaded as a member function, such as the value assignment operator =. If you want to modify the internal status of an object, you can use the class member function to modify it.

Iii. Principles of Operator Overloading

In this way, Operator Overloading is quite simple. In fact, Operator Overloading also follows some principles:

1. Only the existing C ++ operators can be overloaded in C ++, and new operators cannot be defined by users.

2. The vast majority of operators in C ++ can be overloaded, except for member access Operators., Scope Operator::,Length OperatorSizeofAnd conditional Operators? :.

3. After the operator is overloaded, the number of operation objects (operands) of the operator cannot be changed. For example, "+" is an operator that implements two operands. It is still a binary operator after being overloaded.

4. Overloading does not change the original priority of the operator and the original combination.

6. Operator Overloading cannot all be pre-defined basic data in C ++. The purpose of this operation is to prevent users from modifying the operator properties used for basic data types.

 

4. Why Operator Overloading?

So many principles need to be followed for operator overloading, why is Operator overloading required? Why didn't I write an add () function instead of operator + ?? In my opinion, the reason why C ++ must support Operator Overloading is to unify operations with built-in data types, such as c = a + B and c = add (a, B ), which one looks more intuitive? It is obviously the former. At the same time, we hope that the data types defined by ourselves can be as convenient as the built-in data types int and double. It may be a bit difficult to give an example of this addition. Now the operators that add heavy loads are [], <, ^, |, and so on? In this case, what member functions should we use instead ?? What is the effect after replacement? What will this function do at a glance ??

 

Thinking: There are two types of ++ operators in C ++. How can we distinguish between pre-auto-increment and post-auto-increment?

 

 

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.