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
Post A Comment:
0 comments: