Write a program for overloading operator ++ and operator -- using friend functions.

Program Code:
#include<iostream>
using namespace std;
class book
{
private:
int x,y;
public:
book(int a,int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<" \n value of x :"<<x<<endl;
cout<<"\n value of y:"<<y<<endl;
}
friend book operator ++(book &abc);
friend book operator --(book &abc);
};
book operator ++(book &abc)
{
abc.x++;
abc.y++;
return abc;
}
book operator –(book &abc)
{
abc.x--;
abc.y--;
return abc;
}
void main()
{
book b1(5,25);
clrscr();
b1.showdata();
++b1;
b1.showdata();
--b1;
b1.showdata();
}

Output:
the value of x:5
the value of y:25
the value of x:6
the value of y:26
the value of x:5
the value of y:25


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: