forked from activeloopai/deeplake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OPERATOR OVERLOADING.txt
54 lines (46 loc) · 1.19 KB
/
OPERATOR OVERLOADING.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
class complex {
double re; double im;
public:
complex(double r=0,double i=0) : re(r) , im(i)
{
//cout<<"CONSTRUCTOR CALLED";
}
~complex() {}
friend complex operator+(const complex& a,const complex& b) //1
{
return complex(a.re+b.re,a.im+b.im);
}
friend complex operator+(const complex& a, double d) //2
{
complex c(d);
return a+c; //1 is called
}
friend complex operator+(double d, const complex& a) //3
{
complex b(d);
return a+b; // 1 is called
}
friend istream& operator >>(istream & is,complex& a) //4
{
is>>a.re>>a.im;
return is;
}
friend ostream& operator <<(ostream & os,complex& a) //5
{
os<<a.re<<" +i"<<a.im<<endl;
}
void print()
{
cout<<re<<" + i"<<im<<endl;
}
};
int main() {
complex c1(2.67,7.25),c2(1.43,2.41),c3 , c4;
c3=c1+c2; c3.print(); //1
c3 = 1.34 + c2; c3.print(); //3
c3 = c1 + 6.79; c3.print(); //2
cin>>c4>>c2; c4.print(); //4
cout<<c4<<c2; //5
}