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 ++
5 ★ | 1 Vote
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
6 ways to fix the error 'PowerPoint found a problem with the content'
Sample painting of Hanoi in my heart, simple landscape
PowerPoint 2016: Working with lists in PowerPoint
How to Paste an Image into a Selection in Photoshop
Europe develops satellite internet system to replace Starlink
Intel releases new driver update package with 6GHz optimization and many other improvements