You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<iostream>usingnamespacestd;classComplex
{
private:int a,b;
public:voidsetData(int x, int y)
{ a=x; b=y; }
voidshowData()
{ cout << "\na = " << a << "\nb = " << b << endl; }
friend istream& operator>>(istream&, Complex&);
//cout and cin are defined in ostream and instream Classes //but we cannot make an object of these classes //therefore we will use reference of thatfriend ostream& operator<<(ostream&, Complex);
};
istream& operator>>(istream &din, Complex &c)
{
cin >> c.a >> c.b;
return din; //return so that we can use cascading i.e cin >> c1 >> c2;
}
ostream& operator<<(ostream &dout, Complex c)
{
cout << "\na = " << c.a << "\nb = " << c.b << endl;
return dout;
}
intmain()
{
Complex c1;
cout << "Enter a complex Number : ";
cin >> c1; //for this to work we will have to overload >> operator to work with Complex class
cout << c1;
return0;
}
Member Function of one class can be friend to other.
#include<iostream>usingnamespacestd;classA
{
private:int a;
public:voidfun()
{
cout << "in Class A" << endl;
}
};
classB
{
private:int b;
public:friendvoidA::fun();
};
intmain()
{
A obj1;
B obj2;
return0;
}