/*C++ program for unary increment (++) and decrement (--) operator overloading.*/ #include<iostream> using namespace std; class NUM { private: int n; public: //function to get number void getNum(int x) { n=x; } //function to display number void dispNum(void) { cout << "value of n is: " << n; } //unary ++ operator overloading void operator ++ (void) { n=++n; } //unary -- operator overloading void operator -- (void) { n=--n; } }; int main() { NUM num; num.getNum(10); ++num; cout << "After increment - "; num.dispNum(); cout << endl; --num; cout << "After decrement - "; num.dispNum(); cout << endl; return 0; }
Output
After increment - value of n is: 11 After decrement - value of n is: 10
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Unary increment (++) and decrement (--) operator overloading program in c++.
Output
After increment - value of n is: 11 After decrement - value of n is: 10need an explanation for this answer? contact us directly to get an explanation for this answer