-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_BINARY_OPERATOR_OVERLOADING.cpp
95 lines (77 loc) · 1.8 KB
/
2_BINARY_OPERATOR_OVERLOADING.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
using namespace std;
class complex
{
private:
int real;
int img;
public:
complex() //default constructor
{
real=0;
img=0;
}
complex (int r , int i) //parameterize constructor
{
real=r;
img=i;
}
complex operator +(complex t) //passed by value // function will store c2 value
//c2 passed by value c3 calls the function
{
// cout<<real<<"+"<<img<<"i"<<"\n"; it will print 5+5i c2 object
complex temp1; //because u cant return 2 value this stores the final value
temp1.real=real+t.real;
temp1.img=img+t.img;
return(temp1); //return 2 values to c1=c2+c3
}
/* complex operator -(complex t)
{
complex temp1;
temp1.real=real-t.real;
temp1.img=img-t.img;
return(temp1);
}
*/
complex operator *(complex m) //passed by value // complex m will store c3 value
{
complex temp2;
temp2.real=(real*m.real)-(img*m.img);
temp2.img=(real*m.img)+(img*m.real);
return(temp2);
}
friend complex operator - (complex&x,complex&y);
friend ostream &operator<<(ostream&write ,complex&t);
//& operator because << is not in the class we need it to call by ref
};
ostream &operator<<(ostream&write,complex&t)
{
write<<"\n"<<t.real<<"+"<<t.img<<"i";
}
complex operator - (complex&x,complex&y)
{
complex temp1;
temp1.real=x.real-y.real;
temp1.img=x.img-y.img;
return(temp1);
}
int main()
{
cout<<"\ndefault complex no is";
complex c1;
cout<<c1;
cout<<"\nsecond complex no is ";
complex c2(1,1);
cout<<c2;
cout<<"\nthird complex no is ";
complex c3(1,1);
cout<<c3;
cout<<"\n-----------------------\n";
c1=c2+c3;
cout<<"\naddition - "<<c1;
c1=c2-c3;
cout<<"\nsubtraction - "<<c1;
c1=c2*c3;
cout<<"\nmultiplication - "<<c1;
return(0);
}