Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 68099f3

Browse files
overloading the insertion operator
1 parent 8f3dcc3 commit 68099f3

File tree

1 file changed

+48
-0
lines changed
  • Learn_CPP_Programming_Deep_Dive/Section 12 Operator Overloading/Overloading_the_insertion_operator

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Complex
6+
{
7+
private:
8+
int real;
9+
int imaginary;
10+
11+
public:
12+
Complex(int r=0, int i=0): real(r), imaginary(i)
13+
{
14+
cout<<"Parameterized constructor launched here >> "<<real<<" +i* "<<imaginary<<endl;
15+
}
16+
17+
void display()
18+
{
19+
cout<<real<<" +i* "<<imaginary<<endl;
20+
}
21+
22+
friend void display(Complex c);
23+
24+
friend ostream & operator<<(ostream &out, Complex &c);
25+
26+
};
27+
28+
ostream &operator<<(ostream &out, Complex &c)
29+
{
30+
out<<c.real<<" +i* "<<c.imaginary<<endl;
31+
return out;
32+
}
33+
34+
void display(Complex c)
35+
{
36+
cout<<c.real<<" +i* "<<c.imaginary<<endl;
37+
}
38+
39+
int main(void)
40+
{
41+
Complex c1(45,46), c2(12,4);
42+
43+
c1.display();
44+
cout<<c1<<endl;
45+
cout<<c2<<endl;
46+
47+
return 0;
48+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /