I. Operator Overloading --- providing different semantics for operators
struct Complex{int a;int b;};Complex c1={1,2};Complex c2={4,5};
If you want to directly add C1 + C2 to C1 and C2, the direct method in C language is to use functions.
Using Operator Overloading in C ++ to implement C1 + C2;
The essence of Operator Overloading in C ++:
*: Extension operator using the operator keyword in C ++
*: Operator is essentially implemented through function overloading.
Complex operator+ (const Complex& c1,const Complex& c2){<span></span>Complex ret={0,0};ret.a =c1.a+c2.a;ret.b =c1.b+c2.b;return ret;}
Operator + (C1, C2) <=> C1 + C2
Ii. youyuan in C ++
*: The private statement prevents class members from being accessed by the outside world.
*: You can use the friend keyword to obtain the permission of the external category.
(1)
ostream operator<<(ostream& out,const Complex& c){out<<c.a<<"+"<<c.b<<"i";return out;}
(2) member function overloading of operator +
Complex Complex::operator+ (const Complex& c){Complex ret;ret.a = a+c.a;ret.b = b+c.b;return ret;}
Iii. Reload comparison with member functions and global functions Use member function overload:
*: One parameter is missing compared to the global function overload operator (friend,
*: The friend keyword is not required.
How to choose to use global function overload and member function overload Operators
*: When the class cannot be modified as a parameter, use the global function for overload.
For example: Friend ostream operator <(ostream out, const complex & C );
*; =, [],-> Operator can only be overloaded by member functions.
(3) [] Overloading
int& Array::operator[](int i){ return mSpace[i];}
(4) = overload
Array& Array::operator= (const Array& obj){delete[] _space;_length = obj._length;_space = new int[_length];for(int i=0;i<_length;++i)_space[i] = obj._space[i];return *this;}
(5) = heavy load
bool Array::operator==(const Array& obj)
Note: (1) The C ++ compiler provides the default value assignment operator for each class.
(2) The default value assignment operator is simply copying values.
(3) When a class contains pointer member variables (such as a string class), the value assignment operator must be overloaded.
Iv. Overloading of ++ Operators
(1) The ++ operator has only one parameter.
(2) ++ operators include prefixes and suffixes.
Solution:
In C ++, a placeholder parameter is used to distinguish between prefix operators and suffix operators.
(1) heavy load of OBJ ++
The meaning of complex operator ++ (INT) // OBJ ++ {// int mainly distinguishes prefix and suffix complex ret = * This; A ++; B ++; return ret ;}
(2) Overloading of ++ OBJ
Complex operator++() // ++obj{++a;++b;return *this;}
Summary:
(1) The essence of Operator Overloading is function overloading, which extends the semantics of operators. (2) The friend keyword allows access permissions to the global function development class and development class.
(3) =, [],-> the operator can only be overloaded by member functions
(4) ++ operator uses an int parameter to distinguish between prefix and suffix Overloading
9-Operator Overloading