-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathoperatorOverloading.cpp
More file actions
61 lines (46 loc) · 842 Bytes
/
operatorOverloading.cpp
File metadata and controls
61 lines (46 loc) · 842 Bytes
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
#include <iostream>
using namespace std;
//Operator Overloading Concept
class Complex{
private:
int real;
int img;
public:
Complex(int r, int i){
real= r;
img= i;
}
void print(){
cout<< real<< " + "<< img<<"i";
cout<< endl;
}
void add(Complex &x){
real += x.real;
img += x.img;
}
//Operator Overloading
void operator+(Complex &x){
real += x.real;
img += x.img;
}
int operator[](string s){
if(s=="img"){
return img;
}
else{
return real;
}
}
};
int main() {
Complex C1(4,7);
Complex C2(2,3);
C1.print();
C2.print();
//C1.add(C2);
cout<< C1["img"];
cout<< endl;
C1 + C2;
C1.print();
C2.print();
}