Load operator ++ and - in C ++
The incremental operators (++) and the reduced operator (-) are the two important unary operators available in C ++.
The incremental operators (++) and the reduced operator (-) are the two important unary operators available in C ++.
The following example illustrates how to overload the increment operator (++) with the use of the prefix as well as the suffix. Similarly, you can also overload the reduce operator (-) in C ++:
#include using namespace std ; class ThoiGian { private : int gio ; // tu 0 toi 23 int phut ; // tu 0 toi 59 public : // phan khai bao cac constructor can thiet ThoiGian (){ gio = 0 ; phut = 0 ; } ThoiGian ( int h , int m ){ gio = h ; phut = m ; } // phuong thuc de hien thi thoi gian void hienthiTG () { cout << "Gio: " << gio << " Phut: " << phut << endl ; cout << "-----------------------" << endl ; } // nap chong toan tu ++ (tien to) ThoiGian operator ++ () { ++ phut ; // tang doi tuong nay if ( phut >= 60 ) { ++ gio ; phut -= 60 ; } return ThoiGian ( gio , phut ); } // nap chong toan tu ++ (hau to) ThoiGian operator ++( int ) { // luu giu gia tri ban dau ThoiGian T ( gio , phut ); // tang doi tuong nay ++ phut ; if ( phut >= 60 ) { ++ gio ; phut -= 60 ; } // tra ve gia tri return T ; } }; int main () { ThoiGian T1 ( 6 , 59 ), T2 ( 19 , 24 ); ++ T1 ; // tang T1 T1 . hienthiTG (); // hien thi T1 ++ T1 ; // tang T1 mot lan lua T1 . hienthiTG (); // hien thi T1 T2 ++; // tang T2 T2 . hienthiTG (); // hien thi T2 T2 ++; // tang T2 mot lan lua T2 . hienthiTG (); // hien thi T2 return 0 ; }
Compiling and running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous lesson: Load the Input / Output operator stack in C ++
Next lesson: Load the assignment operator stack in C ++
Update 25 May 2019
You should read it
- Overload the one-seat operator in C ++
- Operator in programming C
- Overload subscript operator [] in C ++
- Operator overload and Load overlap in C ++
- The '+' operator in SQL Server
- Operator overloading in C #
- Overload class member access operator (->) in C ++
- Overload binary operators in C ++
- Load the Input / Output operator stack in C ++
- Load the stack of assignment operators in C ++
- How to Get Compensation from a BUI Accident
- Stack operator in Python
Maybe you are interested
Greatly transforming loose-fitting jeans into extremely simple prints at home To protect the hearing, clean the earpiece Instructions for making backup batteries from an orange, charging nearly 40% of the battery for iPhone Scientists develop new tissue regeneration technology Elon Musk released 8 photos of the first human journey to Mars in the future Watch Elon Musk's presentation on the plan to conquer Mars