Table of Contents
Increase Operator in C ++: Load Operator ++ and - in C ++ is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
The incremental operators (++) and the reduced operator (-) are the two important unary operators available in C ++.
the example below 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 ++
Frequently Asked Questions
What is Increase Operator in C ++: Load Operator ++ and - in C ++?
The incremental operators (++) and the reduced operator (-) are the two important unary operators available in C ++.
Why is Increase Operator in C ++: Load Operator ++ and - in C ++ important?
A clear understanding of Increase Operator in C ++: Load Operator ++ and - in C ++ helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
How should beginners approach Increase Operator in C ++: Load Operator ++ and - in C ++?
Start with the fundamental concepts, follow the examples step by step, and test each change in a safe environment before applying it to important systems or data.
Was this article helpful?
Your feedback helps us improve.
![Overload Subscript Operator [][] in C ++](/img/no-image.png)
Reader Comments 0
Sign in with email or Google to join the discussion.