In Operator Overloading we define a new meaning for a C ++ operator. This also changes the way the operator works. For example, the + operator can be used to concatenate two strings.
#include <iostream>
using namespace std;
class StringConcatenation {
private:
string str;
public:
StringConcatenation(string s) {
str = s;
}
StringConcatenation operator+(StringConcatenation const &obj) {
StringConcatenation result = str + obj.str;
return result;
}
void print() {
cout << str << endl;
}
};
int main() {
StringConcatenation obj1("Hello"), obj2("World");
StringConcatenation obj3 = obj1 + obj2;
obj3.print();
return 0;
}
Hello World