C + + overloaded self increasing and self decreasing operators
Self increasing operator + + and self decreasing operator – both are unary operators, and their pre and post forms can be overloaded.
With the basis of operator overloading introduced earlier, we will directly explain the methods of overloading self increasing and self decreasing operators in the form of examples.
#include <iostream> #include <iomanip> using namespace std; class stopwatch { public: stopwatch(){ min = 0; sec = 0;} stopwatch& operator=(const stopwatch& another); void setzero() { min = 0; sec = 0; } stopwatch run(); // function stopwatch operator++(); // ++ i stopwatch operator++(int); // i ++ friend ostream & operator<<( ostream &, const stopwatch &); private: int min; //minute int sec; //second }; stopwatch stopwatch::run() { ++ sec; if( sec == 60 ) { min ++; sec = 0; } return * this; } stopwatch& stopwatch::operator=(const stopwatch& a) { if( this != &a ) { if(min >= 0 ) { min = a.min; sec = a.sec; } else { min = 0; sec = 0; } } return *this; } stopwatch stopwatch::operator++() { return run(); } stopwatch stopwatch::operator++(int n) { stopwatch s = *this; run(); return s; } ostream & operator<<( ostream & out, const stopwatch & s) { out<< setfill('0')<< setw(2) << s.min << ":" <<setw(2) << s.sec; return out; } int main() { stopwatch s1, s2; s1 = s2++; cout << " s1 "<< s1 <<endl; cout << " s2 "<< s2 <<endl; cout << "**************************" << endl; s1++; s2++; cout << " s1 "<< s1 <<endl; s1++; cout << " s1 "<< s1 <<endl; cout << " s2 "<< s2 <<endl; cout << "**************************" << endl; s1.setzero(); s2.setzero(); s1 = ++ s2; cout << " s1 "<< s1 <<endl; cout << " s2 "<< s2 <<endl; return 0; }
In this example, we define a simple stopwatch class. This class has two private member variables min and sec, representing minutes and seconds respectively. The setzero() member function declared in the class is used to reset the stopwatch, and the run() function is used to describe the action of the second hand moving forward for one second. Then there are three operator overloaded functions. The first two are overloaded auto increment operators, and the last one is overloaded output operators.
Let's take a closer look at the specific implementation of each function:
1. First look at the run() function, which makes the second hand increase automatically at the beginning. If the self increase result is equal to 60 at this time, it should carry, add 1 to the minute and set the second hand to zero;
2. Let's take another look at the operator + + () function. This function implements the pre form of self increment, so it can directly return the operation result of the run() function.
3. for operator++(int n) function, this is a self increasing postposition form. The return value of the self added post form is the object itself, but after that object is used again, the object is self added, so in the function body of the function, the object is first saved, then the run() function is called, and the previously saved object is returned. In this function, the parameter n has no meaning. It exists only to distinguish whether it is a pre or post form.
4. Finally, we also overloaded the output operator so that we can print the timing results. In the function body, we used the output control functions, which will be introduced in the chapter of input and output flow.
Compared with the running results of the main function analysis program, the main function defines two objects s1 and s2 at the beginning, and the first operation s1 = s2 + +; The post form is adopted, which can be understood as s1 = s2 and s2 increases automatically. The output result is that s1 is set to zero and s2 increases automatically for one second. After that, both objects are cleared. After clearing, s1 = + s2;, It can be understood as s2 self increment and assign the self increment result to s1. In this way, both objects will self increment for one second.
reference
C + + overloaded self increasing and self decreasing operators