Load operator ++ and - 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 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 ++
May be interested
- Load the stack of assignment operators in C ++you can overload the assignment operator (=) as you can with other operators in c ++ and it can be used to create an object like copying constructors.
- Load the operator stack to call the function () in C ++c ++ function call () can be overloaded for objects of class type. when you overload (), you are not creating a new way to call a function.
- Overload subscript operator [] in C ++the subscript operator [] in c ++ is often used to access array elements. this operator can be overloaded to enhance existing functionality in arrays in c ++.
- Overload class member access operator (->) in C ++the class member access operator (->) can be overloaded, but it's quite complicated.
- Data abstraction in C ++data abstraction involves only providing the necessary information to the outside and hiding their basic details, for example, to represent the information needed in the program without displaying the details. details about them.
- Calculate closure in C ++encapsulation is a concept of object-oriented programming that binds data and functions that manipulate that data, and keeps them safe by preventing obstruction and external abuse. . closedness leads to the important oop concept data hiding.