I've overloaded the pre-increment operator using friend function. In the overloaded friend function, the value of the variable is showing correct. But that value is not shown in display function, why?
#include <iostream>
using namespace std;
class Rectangle {
public:
int breadth;
public:
void read();
void display();
friend void operator ++(Rectangle r1);
};
void Rectangle::read()
{
cout << "Enter the breadth of the Rectangle: ";
cin >> breadth;
}
void operator++(Rectangle r1)
{
++r1.breadth;
cout<<r1.breadth<<endl; //correct result
}
void Rectangle::display()
{
cout<<breadth<<endl; // not showing pre-incremented value, why ???
}
int main()
{
cout<<"Unary Operator using Friend Function \n";
Rectangle r1;
r1.read();
++r1;
cout << "\n breadth of Rectangle after increment: ";
r1.display();
return 0;
}