C ++ ------ Operator Overloading

Source: Internet
Author: User

C ++ ------ Operator Overloading
① What is Operator overload? What is the operator overload of C ++? In fact, operators give it a new meaning or multiple meanings. Let it have another new feature. Why is Operator overloading required? To realize the class polymorphism, in the operator overload program, we can use "+" and "-" to add and subtract integers, single precision, double precision, and pointers, for example: 1 int a = 1 + 2; // Add 2 double d = 1.0 + 2.54 to the integer; // Add 3 int por [10] to the double precision; 4 int * p = por; 5 p = p-1; // the pointer minus 1 ② Operator Overloading comes from the computer's memory. The storage format of integers and floating-point numbers is different, the computer processes the addition and subtraction operations differently, and pointer-1 is not a simple address-1. In C ++, the compiler can solve these problems. It can decide how to handle their operations based on the types on both sides of the expression, and finally decide what operations to execute. Therefore, you can perform operations on different types of operators in C ++. Real-world problems: the plural computation ------- many kids may forget the plural. let's take a look at what a plural is. The plural is the number a + bi that can be written as follows. Here, a and B are real numbers, and I is a virtual number unit. In the plural a + bi, a is called the real part of the plural, B is called the imaginary part of the plural, and I is called the virtual unit. When the imaginary part is equal to zero, this plural value is a real number. When the imaginary part is not equal to zero, this plural value is called a virtual number. If the real part of the plural is equal to zero, it is called a pure virtual number. From the above we can see that the plural set contains the real number set and is the expansion of the real number set. The plural number was introduced for the first time in the 16th century by the Italian Milan scholar Karan. After the work of dalant, momoval, Euler, and Gauss, this concept was gradually accepted by mathematicians. (From: Baidu encyclopedia) Good, come to the question: for example, two plural: S1 = (1 + 2i) S2 = (6 + 1.2i) in mathematics, how do we calculate the sum of two plural values S = S1 + S2 = (7 + 3.2i) (the sum of real and real parts, and the sum of virtual and virtual parts) in C ++? All the reloads that we need to use operators. ③ Syntax of the operator function that is reloaded in the program: The function syntax of the overload operator is generally in the following format: <return value type> # operator name (table of parameters) {// reload processing of operators} operator is a keyword used to define overloaded operator functions. # represents the operators to be reloaded. Example: Complex operator + (const Complex & a, const Complex & B ){......} this function can be used to add two Complex objects. In the following example, copy the Code 1 int main () {2 Complex a (10, 7), B (3, 5); 3 Complex c = a + B; 4 Complex d = a-B; 5 Complex e = a + B-d; 6 cout <"c ="; c. print_complex (); 7 cout <"d ="; d. print_complex (); 8 cout <"e ="; e. print_complex (); 9 int x = 100, y = 200, z; 10 z = x + y; 11 cout <"z =" <z; 12} copy the code and use a member function to reload the + and-operator operations of Complex. Copy code 1 # include <iostream> 2 using namespace std; 3 class Complex {4 private: 5 double rpart, ipart; 6 public: 7 Complex () {rpart = ipart = 0.0;} 8 Complex (double rp, double ip) {9 rpart = rp; ipart = ip; 10} 11 Complex operator + (const Complex & c) {12 Complex temp (rpart + c. rpart, ipart + c. ipart); 13 return temp; 14} 15 16 Complex operator-(const Complex & c); 17 void print_complex () 18 {19 cout <"(" <rpart <',' <ipart <"I)" <endl; 20} 21 }; 22 // out-of-class implementation operator-function 23 Complex: operator-(const Complex & c) {24 Complex temp (rpart-c.rpart, ipart-c.ipart); 25 return temp; 26}

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.