-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass9.cpp
More file actions
44 lines (40 loc) · 847 Bytes
/
Copy pathclass9.cpp
File metadata and controls
44 lines (40 loc) · 847 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
//operator overloading :- when an operator is overloaded with multiple jobs
// this is known as compile time polymorphism
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class complex
{
private:
int a,b;
public:
void setData(int x,int y)
{
a = x; b = y;
}
void showData()
{
cout<<"a = "<<a<<" b = "<<b<<endl;
}
//creating operator overloading
complex operator +(complex c)
{
complex t ;
t.a = a+c.a;
t.b = b+c.b;
return t;
}
};
int main(int argc, char const *argv[])
{
system("cls");
complex c1,c2,c3;
c1.setData(3,7);
c2.setData(10,13);
// c3 = c1.operator+(c2); //this is valid syntax
c3 = c1 + c2; //this is shortcut of obove
c3.showData();
system("pause");
return 0;
}