Skip to content

Commit adf8bff

Browse files
Operator Overloading
1 parent eb6fad5 commit adf8bff

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

Operator_Overloading.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include<iostream>
2+
#define sp " "
3+
using namespace std;
4+
5+
class Demo{
6+
7+
int x, y;
8+
public:
9+
10+
// Default Constructor
11+
Demo(){
12+
x = -1;
13+
y = -1;
14+
}
15+
16+
// Paramterised Constructor
17+
Demo(int a, int b){
18+
this->x = a;
19+
this->y = b;
20+
}
21+
/*
22+
// Copy Constructor
23+
Demo(Demo& obj){
24+
x = obj.x;
25+
y = obj.y;
26+
}
27+
*/
28+
void getNumbers(){
29+
cout<<x<<sp<<y<<endl;
30+
}
31+
32+
Demo operator + (const Demo&);
33+
void print();
34+
};
35+
36+
Demo Demo::operator +(const Demo& obj){
37+
Demo res;
38+
res.x = x + obj.x;
39+
res.y = y + obj.y;
40+
return res;
41+
}
42+
43+
void Demo::print(){
44+
cout<<"x : "<<x<<","<<sp<<"y : "<<y;
45+
}
46+
47+
int main()
48+
{
49+
50+
Demo obj(7, 8);
51+
obj.getNumbers();
52+
Demo obj2(10, 12);
53+
// Demo obj2(obj);
54+
obj2.getNumbers();
55+
Demo result = obj + obj2;
56+
result.print();
57+
58+
return 0;
59+
}

0 commit comments

Comments
 (0)