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
- Operator overloading in C #overloading operator is operator overload. you can redefine or overload most existing operators in c #.
- How to Get Compensation from a BUI Accidentif you were injured in a boating accident, then you can sue the boat operator who caused the injury. for example, the operator may have hit your own boat, or you may have been in the boat with the operator who was intoxicated. either way,...
- How to fix Safari error can not load websites on Macever encountered the problem of not being able to load pages on safari on a mac? obviously your network connection is extremely stable, but a website can't keep loading on safari. here is how to fix this phenomenon.
- Stack operator in Pythonyou can change the meaning of the operator in python depending on the operand used and we call that operator overloading. quantrimang will learn more about this content through the article. invites you to read the track.
- OpenAI Announces Operator, an AI Agent That Automatically Performs Tasks on User's Behalfthe company behind chatgpt is unveiling a new way to use ai. openai has just announced operator, an ai agent that can automatically perform tasks for you.
- EXCEPT operator in SQL Serverthe except operator in sql server is used to return the rows in the first select statement that are not returned in the second select statement.
- Wildcard representation operator in Accesswildcard, also known as a wildcard, can be used to determine the location of a specific item when you don't remember exactly how it was written.
- Guide to speed up website loading in a simple wayaccording to kissmetrics, 47% of users expect a website to load in about 2 seconds, 40% of users will abandon a website if it takes more than 3 seconds to load.
- Operator in JavaScriptwe see the following simple expression: 4 + 5 is equal to 9. here 4 and 5 are operands and '+' called operator - operator.
- UNION operator in SQL Serverthis tutorial explains how to use the union operator in sql server with specific syntax and examples.