Answer to Question #262742 in C++ for Vict

Question #262742

Write a program to overload unary

(++) operator and unary (--) operator using friend function.


1
Expert's answer
2021-11-09T00:24:47-0500
#include<iostream>
using namespace std;
class Overloading
{
    int i,x;
    public:
        Overloading(int m,int n)
        {
            i=m;
            x=n;
        }
    void display()
        {
            cout<<"i="<<i;
            cout<<"\ny="<<x;
        }
    friend void operator --(Overloading &);
    friend void operator ++(Overloading &);
};
void operator --(Overloading &a)
{
    a.i=--a.i;
    a.x=--a.x;
}
void operator ++(Overloading &a)
{
    a.i=++a.i;
    a.x=++a.x;
}
int main()
{
    Overloading a(4,6);
    Overloading b(5,6);
    --a;
    ++b;
    cout<<"Displaying -- unary operator\n";
    a.display();
    cout<<"\nDisplaying ++ unary operator\n";
    b.display();
    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