Answer to Question #257981 in C++ for Amit

Question #257981

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


1
Expert's answer
2021-10-30T00:39:45-0400
#include<iostream>
using namespace std;
class Unary
{
    int x,y;
    public:
        Unary(int m,int n)
        {
            x=m;
            y=n;
        }
    void show()
        {
            cout<<"x="<<x;
            cout<<"\ny="<<y;
        }
    friend void operator --(Unary &);
    friend void operator ++(Unary &);
};
void operator --(Unary &a)
{
    a.x=--a.x;
    a.y=--a.y;
}
void operator ++(Unary &a)
{
    a.x=++a.x;
    a.y=++a.y;
}
int main()
{
    Unary a(4,6);
    Unary b(4,6);
    --a;
    ++b;
    cout<<"Displaying -- unary operator\n";
    a.show();
    cout<<"\nDisplaying ++ unary operator\n";
    b.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