












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
How to overload the stream and extraction operators in object- oriented programming (oop) using the example of a complex class. It covers the use of the stream insertion operator '<<' and the stream extraction operator '>>' and their implementation in the complex class. The document also includes the main program and its output.
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













Complex & rhs);
// Error c1 << cout;c1 << cout << 2; // Errorreturn 0;
(ostream
&^ os){ os^ <<^ ‘(‘
<<^ real<< ‘,’ <<^ img
ostream
&^ operator
(ostream
&^ os,^ const^ Complex&^
c);
const
Note: return typeis NOT^ const
c1(1.01,
c2(0.01,
cout^ << c1^ <<^ endl^ << c2;
Stream Extraction Operator^ ►Overloading
istream
&^ operator
^ (istream
&^ i,^ Complex
c);
Note: this objectis NOT^ const
Stream Extraction Operator istream^
&^ operator
<<^ (istream &^ in,^ Complex
&^ c){ in^ >>^ c.real;in^ >>^ c.img;return^ in; }
Stream Extraction Operator^ Output:^ ( 1.0025 , 0.0241 )
(lhs.img == rhs.img)){return true;} elsereturn false;}
(img != c.img)){return true;} elsereturn false;}