Answer to Question #257980 in C++ for Amit

Question #257980

 Write a program to overload unary (++) operator and unary (--) operator.


1
Expert's answer
2021-10-29T00:55:03-0400
#include<iostream>
using namespace std;
class Unary
{
    int x, y;
 public:
    void input()
    {
        cout<<"Input two intergers: \n";
        cin>>x>>y;
    }
    void operator--()
    {
        x--;
        y--;
    }
    void operator++()
    {
        x++;
        y++;
    }
    void show()
    {
        cout<<"\nX : "<<x;
        cout<<"\nY : "<<y;
    }
};
int main()
{
    Unary U;
    U.input();
    --U;
    cout<<"\nAfter Decrement: ";
    U.show();
    ++U;
    ++U;
    cout<<"\n\nAfter Increment: ";
    U.show();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS