-
Notifications
You must be signed in to change notification settings - Fork 28
/
Operator_Overloading.cpp
52 lines (41 loc) · 1.46 KB
/
Operator_Overloading.cpp
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
#include <bits/stdc++.h>
using namespace std;
/*
Write a C++ program to show concatenation of strings using operator overloading
Operator overloading rules :
-For unary prefix operators, use : MyType& operator++(MyType& obj) example : ++a
-For unary postfix operators, use : MyType& operator++(MyType& obj, int) example : a++ (You don't pass any int, it's just for compiler)
-The overload operator-> must either return a raw pointer or return an object (by reference or by value), for which
operator-> is in turn overloaded. (Later will be explained)
*/
class String{
public:
string str;
String operator + (const String& Obj0) {
String obj;
obj.str = this->str + Obj0.str;
return obj;
}
};
int main() {
/*
Operator Overloading
a+b -> operator+(a,b)
a=b -> operator=(a,b)
c=a+b -> operator=(a, operator+(a,b))
- Operator function can have zero or one arguments
- Atleast one of the arguments must e of user defined type
*/
string fName = "Tony ";
string lName = "Stark";
string fullName = fName + lName;
cout << fullName << endl;
//Let's do this using operator overloading
String fNameObj;
fNameObj.str = "Tony ";
String lNameObj;
lNameObj.str = "Stark";
String fullNameObj = (fNameObj+lNameObj); //Adding two objects of class String
cout << fullNameObj.str << endl;
return 0;
}