-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathOperator_Overloading_UDT_IV.cpp
61 lines (44 loc) · 1.58 KB
/
Operator_Overloading_UDT_IV.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
#include <bits/stdc++.h>
using namespace std;
/*
Operator overloading continued:
Extending operator+
In operator Overloadin UDT-III , we saw that member operator function had limitation to handle cases when left operand is not an object type
of the class.
So, we will now use friend operator function to handle all issues. My Superman!!!!!!!!!!!
*/
class Complex {
private:
double re, im; //They can be private now
public:
Complex(double r = 0, double i = 0): re(r), im(i) {}
void display() {
cout << re << " +j" << im << endl;
}
//friend function can access private members as well
friend Complex operator+(const Complex& a, const Complex& b) { //overload 1
return Complex(a.re + b.re, a.im + b.im);
}
friend Complex operator+(const Complex& a, double d) { //overload 2
Complex b(d); //create temp object and user overload 1
return a+b;
}
friend Complex operator+(double d, const Complex& b) { //overload 3
Complex a(d); //create temp object and user overload 1
return a+b;
}
};
int main() {
/*Using friend operator Function*/
//It Can handle case like MyClass d2 = 2.3 + d2; and can also preserve encapsulation (private members)
Complex d1(2.5, 3.2);
Complex d2(1.6, 3.3);
Complex d3;
d3 = d1 + d2; //left opearand d1 is an object and it will be called as d1.operator+(d2)
d3.display();
d3 = d1 + 6.2;//left opearand d1 is an object and it will be called as d1.operator+(6.2)
d3.display();
d3 = 4.2 + d2; //Yayyyyyyyyyyyy
d3.display();
return 0;
}