Friend function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Friend function

what can i do using friend function...if you have any idea or coding problem relating to this,plz help me.

27th Oct 2018, 3:36 AM
1704086_ibnul
1704086_ibnul - avatar
1 Answer
+ 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.
27th Oct 2018, 4:16 AM
Megatron
Megatron - avatar