#include <iostream.h>
#include <conio.h>

class Distance
{
	public:
	int feet;
	int inch;
	void setData(int x, int y)
	{
		feet=x;
		inch=y;
	}
	void getData()
	{
		cout<<feet<<" feet "<<inch <<" inch"<<endl;
	}
	Distance operator + (Distance &d)
	{
		Distance temp;
		temp.feet=feet+d.feet;
		temp.inch=inch+d.inch;
		if(temp.inch>11)
		{
			temp.feet++;
			temp.inch = temp.inch %12;

		}
		return temp;
	}
	Distance operator - (Distance &d)
	{
		Distance temp;

		temp.feet = feet - d.feet;
		temp.inch = inch -d.inch;

		cout<< temp.feet <<temp.inch;

		if(temp.inch <0)
		{
			temp.feet--;
			temp.inch = (12+inch) -(d.inch) ;
		}
		return temp;

	}

};

void main()
{
	clrscr();
	Distance d1,d2,d3;
	d1.setData( 3,8);
	d2. setData(2,10);
	d3=d1-d2;

	d3.getData();

}
Overloading Binary + and – Operator Using Member Functions in CPP

Leave a Reply

Your email address will not be published. Required fields are marked *