An introduction to operator overloading
Operator overloading is to give some operators new functions, so that they can be used in new data types, not just limited to basic data types.
In fact, when we first started to learn C++, we have already touched operator overloading, and our output data in C++ is in this form
int a = 0; cout << a;
<< This left shift operator is operator overloading. We used this operator to complete the left shift operation before, but in C++ we use it to output data.
[Note]: Overloaded operators are essentially functions !
Operator overloading also requires a return type, a parameter list, and a function body, but unlike a normal function, its name consists of operator plus the operator to be overloaded .
We can only overload existing operators, cannot create new operators , and cannot change the precedence and associativity of the original operators .
[Note]: Not all operators can be overloaded, and the overloaded operators are listed at the end.
The declaration of operator overloading
We first create a Date class.
class Date { public: Date(); //No parameter constructor Date(int hour, int min, int sec);//Constructor with three parameters void print();//Print date private: int m_hour; int m_min; int m_sec; };
Then we use this class to define two Date objects, d1(2,3,4), d2(5,6,7), if we want to add these two dates, and only use the + operator to achieve , then we need to overload the + operator.
Operator overloading as a member function of a class
Date operator+(Date &d); //+ operator overloaded as a member function of the class
Note that the operands of the + operator should be two . Although this function seems to have only one parameter, its first operand is actually bound to the this pointer. If we want to use the overloaded operator, we can write it like this.
Date t1(2,3,4); Date t2(4,5,6); t1+t2;The above t1+t2 is equivalent to writing t1.operator+(t2); obviously t1+t2 is much more convenient, which is the benefit of operator overloading.
Make operator overloading a friend function of a class
friend Date operator+(Date &d1, Date &d2);
Because the friend function is not a member function of the class, there is no this pointer , so we can only talk about both objects as parameters. Then we can also use t1+t2 to add two Date objects.
The difference between the two methods
When overloading a function as a member function of a class, the parameter list in the parameter list will be one less than the formal parameter list using a friend function.
3. Overloading the left shift operator
If we want to use cout << t1 to output the data in t1, we also have to overload the left shift operator, because cout << can only input basic data types, if this operator is not overloaded, it will be compiled report an error.
friend ostream& operator<<(ostream &out, Date &d); //left shift operator overload
The first parameter is a reference to the output stream class object, and the second parameter is a reference to the Date class object. The return type is an ostream reference.
The first operand of the << left shift operator is a member of ostream . We cannot change the members in the ostream class , so we can only use friend functions to declare them .
The right shift operator is similar to the left shift operator overloading.
[Note]: When we overload the left shift and right shift operators, we must use the friend function to declare .
4. Operators that can be overloaded
+ | - | * | / | % | ^ |
& | | | ~ | ! | , | = |
< | > | <= | >= | ++ | -- |
<< | >> | == | != | && | || |
+= | -= | /= | %= | ^= | &= |
|= | *= | <<= | >>= | [] | () |
-> | ->* | new | new[] | delete | delete[] |
Finally, provide the code of the Date class I wrote myself
#include <iostream> using namespace std; class Date { public: Date(); //No parameter constructor Date(int hour, int min, int sec);//Constructor with three parameters void print();//Print date Date operator+(Date &d);//+ operator overloading Date operator-(Date &d);//- operator overloading Date operator=(Date &d);//= operator overloading private: int m_hour; int m_min; int m_sec; //friend function //Because the first parameter is an ostream class object, only friend functions can be used. friend ostream& operator<<(ostream &out, Date &d); //left shift operator overload friend istream& operator >> (istream &in, Date &d);//Right shift operator overload }; //initialization Date::Date() { m_hour = 0; m_min = 0; m_sec = 0; } //initialization Date::Date(int hour, int min, int sec) { m_hour = hour; m_min = min; m_sec = sec; } // print date void Date::print() { cout << this->m_hour << "时" << this->m_min << "分" << this->m_sec << "秒" << endl; } //Convert date to ordinary hours, minutes and seconds Date Date::operator+(Date &d) { Date temp; temp.m_sec = this->m_sec + d.m_sec; temp.m_min = this->m_min + d.m_min; temp.m_hour = this->m_hour + d.m_hour; if (temp.m_sec >= 60) { temp.m_min = temp.m_sec / 60 + temp.m_min; temp.m_sec = temp.m_sec % 60; if (temp.m_min >= 60) { temp.m_hour = temp.m_min / 60 + temp.m_hour; temp.m_min = temp.m_min % 60; } } else if (temp.m_sec < 60 && temp.m_min >= 60) { temp.m_hour = temp.m_min / 60 + temp.m_hour; temp.m_min = temp.m_min % 60; } return temp; } //- operator overloading Date Date::operator-(Date & d) { Date temp; temp.m_hour = this->m_hour - d.m_hour; temp.m_min = this->m_min - d.m_min; temp.m_sec = this->m_sec - d.m_sec; if (temp.m_hour < 0)//If the hour is less than 0, set the time to 0 { temp.m_hour = 0; temp.m_min = 0; temp.m_sec = 0; } else if (temp.m_hour >= 0 && temp.m_min < 0) If the hour > 0 minute is less than 0, the minute and second will be cleared to zero { temp.m_min = 0; temp.m_sec = 0; } else if (temp.m_hour >= 0 && temp.m_min >= 0 && temp.m_sec < 0)//If only seconds are less than 0, clear seconds { temp.m_sec = 0; } return temp; } Date Date::operator=(Date &d) { this->m_hour = d.m_hour; this->m_min = d.m_min; this->m_sec = d.m_sec; return *this; } // output normal hours, minutes and seconds ostream & operator<<(ostream &out, Date &temp) { if (temp.m_sec >= 60) { temp.m_min = temp.m_sec / 60 + temp.m_min; temp.m_sec = temp.m_sec % 60; if (temp.m_min >= 60) { temp.m_hour = temp.m_min / 60 + temp.m_hour; temp.m_min = temp.m_min % 60; } } else if (temp.m_sec < 60 && temp.m_min >= 60) { temp.m_hour = temp.m_min / 60 + temp.m_hour; temp.m_min = temp.m_min % 60; } out << "Now is: "; out << temp.m_hour << "时" << temp.m_min << "分" << temp.m_sec << "秒"; return out; } istream& operator >> (istream &in, Date &d) { in >> d.m_hour >> d.m_min >> d.m_sec; return in; } intmain() { Date d1(2, 2, 2); Date d2(2, 2, 2); Date d3; d3 = d2 + d1; //d3 = d2.operator+(d1); /*Date d4; d4 = d3;*/ cout << d3 << endl; system("pause"); return 0; }