How we can overload Stream Extraction and Insertion Operators-00444
This subjective question is related to the book/course vu cs301 Data Structures. It can also be found in vu cs301 Mid Term Solved Past Paper No. 2.
Question 1: How we can overload Stream Extraction and Insertion Operators in c++? Give example code for Complex Number Class.
...
friend ostream & operator << (ostream & os, const Complex & c);
};
Stream Insertion operator
// we want the output as: (real, img)
ostream & operator << (ostream & os, const Complex & c){
os << "(" << c.real << "," << c.img << ")?;
return os;
}
Overloading Stream Extraction Operator
class Complex{
...
friend istream & operator >> (istream & i, Complex & c);
};
Answer:
Overloading Stream Insertion Operator
class Complex{...
friend ostream & operator << (ostream & os, const Complex & c);
};
Stream Insertion operator
// we want the output as: (real, img)
ostream & operator << (ostream & os, const Complex & c){
os << "(" << c.real << "," << c.img << ")?;
return os;
}
Overloading Stream Extraction Operator
class Complex{
...
friend istream & operator >> (istream & i, Complex & c);
};