-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperator_Overloading.cpp
More file actions
59 lines (49 loc) · 1011 Bytes
/
Operator_Overloading.cpp
File metadata and controls
59 lines (49 loc) · 1011 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
#include<iostream>
#define sp " "
using namespace std;
class Demo{
int x, y;
public:
// Default Constructor
Demo(){
x = -1;
y = -1;
}
// Paramterised Constructor
Demo(int a, int b){
this->x = a;
this->y = b;
}
/*
// Copy Constructor
Demo(Demo& obj){
x = obj.x;
y = obj.y;
}
*/
void getNumbers(){
cout<<x<<sp<<y<<endl;
}
Demo operator + (const Demo&);
void print();
};
Demo Demo::operator +(const Demo& obj){
Demo res;
res.x = x + obj.x;
res.y = y + obj.y;
return res;
}
void Demo::print(){
cout<<"x : "<<x<<","<<sp<<"y : "<<y;
}
int main()
{
Demo obj(7, 8);
obj.getNumbers();
Demo obj2(10, 12);
// Demo obj2(obj);
obj2.getNumbers();
Demo result = obj + obj2;
result.print();
return 0;
}