Operator overloading in C++ (Cout and Cin)
#include<iostream.h>
#include<conio.h>
class distance
{
int feet,inches;
public:
distance()
{
}
distance(int f,int i)
{
feet=f;
inches=i;
}
friend ostream &operator<<(ostream &output,const distance &d)
{
output<<"F: "<<d.feet<<" I: "<<d.inches;
return output;
}
friend istream &operator>>(istream &input, distance &d)
{
input>>d.feet>>d.inches;
return input;
}
};
void main()
{
distance d1(11,10),d2(5,11),d3;
clrscr();
cout<<"Enter the value of object: "<<endl;
cin>>d3;
cout<<"First Distance: "<<d1<<endl;
cout<<"Second Distance: "<<d2<<endl;
cout<<"Third Distance: "<<d3;
getch();
}
#include<conio.h>
class distance
{
int feet,inches;
public:
distance()
{
}
distance(int f,int i)
{
feet=f;
inches=i;
}
friend ostream &operator<<(ostream &output,const distance &d)
{
output<<"F: "<<d.feet<<" I: "<<d.inches;
return output;
}
friend istream &operator>>(istream &input, distance &d)
{
input>>d.feet>>d.inches;
return input;
}
};
void main()
{
distance d1(11,10),d2(5,11),d3;
clrscr();
cout<<"Enter the value of object: "<<endl;
cin>>d3;
cout<<"First Distance: "<<d1<<endl;
cout<<"Second Distance: "<<d2<<endl;
cout<<"Third Distance: "<<d3;
getch();
}
Comments
Post a Comment