15. C ++-Operator Overloading and 15. c Operator Overloading
First, recall the previously learned function overloading.
Function overload
- The essence of function Overloading isIndependent Functions
- PassFunction NameAndFunction ParametersTo determine the function call
- You cannot directly obtain the entry address of the overload function by using the function name.
- Function overloading must occurSame ScopeMedium
Function overload in the class
- Static member functionsBenefitsCommon member functionsCreate a reload relationship
- Global FunctionsAnd member functionsCannot constituteReload Link
Operator overload(Operator)
What is Operator overload?
As we all know, in C, there are'+ ,-,*,/'These operators are used to perform common variable operations.
Since C ++ is object-oriented and most of the variables encountered are objects, the operators in C are optimized,Make them have the ability to overload.ObjectAvailable'+ ,-,*,/'.
Operator overload isIn function mode.
Operator overload Definition
Operator overload, throughOperator keywordDefine before the function:
[Return type] operator [operator to be overloaded] (function parameter ){//......}
There are several methods to overload operators: global operators overload functions and global operators overload functions.
The compiler first checks whether the operation is an object and then looks for it from the class.Member operator overload Function, If not found, it will go to the global searchGlobal operator overload function.
Note:
- Operator overloadCannot be changedOriginal OperatorPriority
- Operator overloadCannot be changedOperandNumber
- The operator overload parameter is generally setConst class_name &Type (if only set to const class_name, a temporary object will be generated)
- In C ++, some operators mustObject supportSo only member functions can be assigned (=), subscript ([]), call (), and member access arrow (-> ):
Test t3 = t2; // call: Test t3.operator = (t2); this pointer is used to replace the number t3 on the left.
- Some operators can be usedMember operator overload FunctionCan also be usedGlobal operator overload Function, Such as addition (+ ):
When you set it to a global operator to overload a function, execute
Test t3 = t1 + t2; // call: Test t3 = operator + (t1, t2 );
When set as a member operator to overload a function, execute
Test t3 = t1 + t2; // call: Test t3 = t1.operator + (t2); // this pointer is used to replace the number t1 on the left.
Preliminary Test
1. NextGlobal operator overload FunctionExample:
# Include "stdio. h "class Test {int x; int y; public: Test (int x = 0, int y = 0) {this-> x = x; this-> y = y;} int getx () {return x;} int gety () {return y;} friend Test operator + (const Test & t1, const Test & t2); // declare a friend function. You can use the private member variable}; Test operator + (const Test & t1, const Test & t2) // reload {Test ret; ret. x = t1.x + t2.x; ret. y = t1.y + t2.y; return ret;} int main () {Test t1 (1, 3); Test t2 (2, 4); Test t3 = t1 + t2; // actually called: test t3 = operator + (t1, t2); printf ("t3.x: % d t3.y: % d \ n", t3.getx (), t3.gety ()); test t4 = operator + (t1, t3); // t4 = t1 + t3 printf ("t4.x: % d t4.y: % d \ n", t4.getx (), t4.gety (); return 0 ;}
Print result:
t3.x:3 t3.y:7t4.x:4 t4.y:10
2. ChangeMember operator overload FunctionExample:
# Include "stdio. h "class Test {int x; int y; public: Test (int x = 0, int y = 0) {this-> x = x; this-> y = y;} int getx () {return x;} int gety () {return y;} Test operator + (const Test & t2) {Test ret; ret. x = this-> x + t2.x; ret. y = this-> y + t2.y; return ret ;}}; int main () {Test t1 (1, 3); Test t2 (2, 4); Test t3 = t1 + t2; // call: Test t3 = t1.operator + (t2); printf ("t3.x: % d t3.y: % d \ n", t3.getx (), t3.gety ()); test t4 = t1.operator + (t3); // t4 = t1 + t3 printf ("t4.x: % d t4.y: % d \ n", t4.getx (), t4.gety ()); return 0 ;}
Print result:
t3.x:3 t3.y:7t4.x:4 t4.y:10
Deep understanding
Because C ++,There is no complex comment.And I learned it again.Operator overloadSo next we will use the operator overloadImplement plural classes
The plural class should have
Two members
Real Department a and virtual department B
Operator
+-: Result = two real parts are added and subtracted.
*: Result = (a1 + b1) (a2 + b2) = (a1 * a2-b1 * b2) + (a2 * b1 + a1 * b2 );
/: Result = (a1 + b1)/(a2 + b2) = (a1 * a2 + b1 * b2)/(a2 * a2 + b2 * b2) + (b1 * a2-a1 * b2)/(a2 * a2 + b2 * b2)
Comparison operator:= ,! =
Value assignment operator: =
Evaluate the modulo member function:Equal to the arithmetic square root of a ^ 2 + B ^ 2
Therefore, the operator overloading of the plural class involves the following:
1. Write the header file Complex. h:
#ifndef __COMPLEX_H#define __COMPLEX_Hclass Complex{private: double a; double b;public: Complex(int a=0,int b=0); Complex operator + (const Complex& t); Complex operator - (const Complex& t); Complex operator * (const Complex& t); Complex operator / (const Complex& t); bool operator == (const Complex& t); bool operator != (const Complex& t); Complex& operator = (const Complex& t); double getModulus(); double getA(); double getB();};#endif
2. Write the source file Complex. cpp.
#include "Complex.h"#include "math.h"Complex::Complex(int a,int b){ this->a = a; this->b = b;}Complex Complex::operator + (const Complex& t){ Complex ret; ret.a = a + t.a; ret.b = b + t.b; return ret;} Complex Complex::operator - (const Complex& t){ Complex ret; ret.a = a - t.a; ret.b = b - t.b; return ret;} Complex Complex::operator * (const Complex& t){ Complex ret; ret.a = (a* t.a - b* t.b ); ret.b = (t.a *b + a* t.b ); return ret;} Complex Complex::operator / (const Complex& t){ Complex ret; ret.a = (a* t.a + b* t.b)/(t.a * t.a + t.b * t.b); ret.b = (b* t.a - a* t.b)/(t.a * t.a + t.b * t.b); return ret;} bool Complex::operator == (const Complex& t){ if((a== t.a)&&(b== t.b)) return true; else return false;} bool Complex::operator != (const Complex& t){ if((a!= t.a)||(b!= t.b)) return true; else return false;}Complex& Complex::operator = (const Complex& t){ if(this != &t) { a = t.a; b = t.b; } return *this;} double Complex::getModulus(){ return sqrt( a*a + b*b);} double Complex::getA(){ return a;} double Complex::getB(){ return b;}
3. Write the test file test. cpp.
#include "stdio.h"#include "Complex.h"int main(){ Complex t1(1,3); Complex t2(2,6); Complex t3=t1+t2; printf("t3.a=%f t3.b=%f\n",t3.getA(),t3.getB()); printf("t3 Modulus:%f\n",t3.getModulus()); Complex t4=t3; printf("t4==t3: %d\n",t4==t3); printf("t4!=t3: %d\n",t4!=t3); printf("t3==t1: %d\n",t3==t1); return 0;}
4. Compile and run
T3.a = 3.000000 t3. B = 9.20.00t3 Modulus: 9.486833t4 = t3: 1 // true t4! = T3: 0 // false t3 = t1: 0 // false