8000 Plus Operator Overloading · DhirajGadekar/Cpp_programming@0fa51de · GitHub
[go: up one dir, main page]

Skip to content

Commit 0fa51de

Browse files
committed
Plus Operator Overloading
1 parent b28e9ec commit 0fa51de

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<iostream>
2+
3+
class Demo {
4+
5+
int x;
6+
int y;
7+
public:
8+
Demo(int x, int y) {
9+
10+
this->x = x;
11+
this->y = y;
12+
}
13+
14+
friend int operator+(const Demo& obj1, const Demo& obj2);
15+
friend int operator+(const Demo& obj1, int data) {
16+
17+
return obj1.x + data;
18+
}
19+
};
20+
21+
int operator+(const Demo& obj1, const Demo& obj2) {
22+
23+
return obj1.x + obj2.y;
24+
}
25+
26+
int main() {
27+
28+
Demo obj1(10,20);
29+
Demo obj2(40,50);
30+
31+
std::cout << obj1 + obj2 << std::endl;
32+
std::cout << obj1 + 100 << std::endl;
33+
34+
return 0;
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<iostream>
2+
3+
class Demo {
4+
5+
int x;
6+
public:
7+
Demo(int x) {
8+
9+
this->x = x;
10+
}
11+
int getData() const {
12+
13+
return x;
14+
}
15+
};
16+
17+
int operator+(const Demo& obj1, const Demo& obj2) {
18+
19+
return obj1.getData() + obj2.getData();
20+
}
21+
int FBDD main() {
22+
23+
Demo obj1(10);
24+
Demo obj2(30);
25+
26+
std::cout << obj1 + obj2 << std::endl;
27+
return 0;
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include<iostream>
2+
3+
class OperatorOverload {
4+
5+
int x;
6+
public:
7+
OperatorOverload(int x) {
8+
9+
this->x = x;
10+
}
11+
int operator+(const OperatorOverload& obj) {
12+
13+
return obj.x + this->x;
14+
}
15+
};
16+
17+
int main() {
18+
19+
OperatorOverload obj1(10);
20+
OperatorOverload obj2(20);
21+
22+
std::cout << obj1 + obj2 << std::endl;
23+
24+
return 0;
25+
}

0 commit comments

Comments
 (0)
0