-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathoperator overloading.cpp
More file actions
56 lines (46 loc) · 954 Bytes
/
operator overloading.cpp
File metadata and controls
56 lines (46 loc) · 954 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
/*
* Note: can't overload these operators :: . .* ?:
* operator overloading is fuction of Polymorphism i.e OOP -- hence operator overloading isn't available in C
*/
#include<iostream>
using namespace std;
class Complex
{
int real,imag;
public:
Complex()
{
real = 0;
imag = 0;
}
Complex(int r, int i)
{
this->real = r; //this
this->imag = i;
}
void show()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
Complex operator +(Complex o) //copy constructor , operator is a keyword
{
Complex c;
c.real = this->real + o.real;
c.imag = this->imag + o.imag;
return c;
}
};
int main()
{
Complex c1(2,3);
cout<<endl<<"First Input"<<endl;
c1.show();
Complex c2(4,3);
cout<<endl<<"Second Input"<<endl;
c2.show();
cout<<endl<<"Sum"<<endl;
Complex c3;
c3 = c1 + c2;
c3.show();
return 0;
}