0
Friend function
what can i do using friend function...if you have any idea or coding problem relating to this,plz help me.
1 Odpowiedź
+ 1
This is the best use i have to come across..
class ComplexNo
{
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{ real = r; imag = i; }
friend ostream & operator << (ostream &out, const Complex &c);
friend istream & operator >> (istream &in, Complex &c);
};
ostream & operator << (ostream &out, const Complex &c)
{
out << c.real;
out << "+i" << c.imag << endl;
return out;
}
istream & operator >> (istream &in, Complex &c)
{
cout << "Enter Real Part ";
in >> c.real;
cout << "Enter Imagenory Part ";
in >> c.imag;
return in;
}
Now
ComplexNo c1,c2;
cin>>c1>>c2;
cout<<c1<<c2;
type operation are possible. so they can be treated as normal variables for input output purposes.