/*

Objective: Overload insertion  << and >> operator for Distance class using Friend Function
Author: Amit Kumar Trivedi

*/

#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;
	}
	friend ostream &amp;operator << (ostream &amp;out, Distance d)
	{
		out<<d.feet<<" ' "<<d.inch<< " ''"<<endl;
		return out;
	}
	friend	istream &amp;operator >> (istream &amp;in, Distance &amp;d)
	{
		in>> d.feet >> d.inch;
		return in;
	}
};
void main()
{
	clrscr();
	Distance d1,d2,d3;
     //	cin >> d1;
     d1.setData(20,20);
     cin>>d2;

	cout<<"you have entered"<<endl;
	cout<<d1<<d2;

	getch();
}

Overloading Insertion (<<) operator and extraction with Distance Class in CPP

Leave a Reply

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