C++ Program to concatenate two strings using Operator Overloading Last Updated : 17 May, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Pre-requisite: Operator Overloading in C++Given two strings. The task is to concatenate the two strings using Operator Overloading in C++. Example: Input: str1 = "hello", str2 = "world" Output: helloworld Input: str1 = "Geeks", str2 = "World" Output: GeeksWorld Approach 1: Using unary operator overloading. To concatenate two strings using unary operator overloading. Declare a class with two string variables.Create an instance of the class and call the Parameterized constructor of the class to initialize those two string variables with the input strings from the main function.Overload the unary operator + to concatenate these two string variables for an instance of the class.Finally, call the operator function and concatenate two class variables. Below is the implementation of the above approach: C++ // C++ Program to concatenate two string // using unary operator overloading #include <iostream> #include <string.h> using namespace std; // Class to implement operator overloading // function for concatenating the strings class AddString { public: // Classes object of string char s1[25], s2[25]; // Parameterized Constructor AddString(char str1[], char str2[]) { // Initialize the string to class object strcpy(this->s1, str1); strcpy(this->s2, str2); } // Overload Operator+ to concat the string void operator+() { cout << "\nConcatenation: " << strcat(s1, s2); } }; // Driver Code int main() { // Declaring two strings char str1[] = "Geeks"; char str2[] = "ForGeeks"; // Declaring and initializing the class // with above two strings AddString a1(str1, str2); // Call operator function +a1; return 0; } Output: Concatenation: GeeksForGeeks Approach 2: Using binary operator overloading. Declare a class with a string variable and an operator function '+' that accepts an instance of the class and concatenates it's variable with the string variable of the current instance.Create two instances of the class and initialize their class variables with the two input strings respectively.Now, use the overloaded operator(+) function to concatenate the class variable of the two instances. Below is the implementation of the above approach: C++ // C++ Program to concatenate two strings using // binary operator overloading #include <iostream> #include <string.h> using namespace std; // Class to implement operator overloading function // for concatenating the strings class AddString { public: // Class object of string char str[100]; // No Parameter Constructor AddString() {} // Parameterized constructor to // initialize class Variable AddString(char str[]) { strcpy(this->str, str); } // Overload Operator+ to concatenate the strings AddString operator+(AddString& S2) { // Object to return the copy // of concatenation AddString S3; // Use strcat() to concat two specified string strcat(this->str, S2.str); // Copy the string to string to be return strcpy(S3.str, this->str); // return the object return S3; } }; // Driver Code int main() { // Declaring two strings char str1[] = "Geeks"; char str2[] = "ForGeeks"; // Declaring and initializing the class // with above two strings AddString a1(str1); AddString a2(str2); AddString a3; // Call the operator function a3 = a1 + a2; cout << "Concatenation: " << a3.str; return 0; } Output: Concatenation: GeeksForGeeks Comment More infoAdvertise with us Next Article How to Concatenate Multiple Strings in C++? B bilal-hungund Follow Improve Article Tags : Technical Scripter C++ Programs GATE CS C++ Technical Scripter 2018 C++-Operator Overloading +2 More Practice Tags : CPP Similar Reads C++ Program to compare two string using pointers Given two strings, compare the strings using pointers Examples: Input: str1 = geeks, str2 = geeks Output: Both are equal Input: str1 = hello, str2 = hellu Output: Both are not equal As their length are same but characters are different The idea is to dereference given pointers, compare values and ad 1 min read How to Concatenate Multiple Strings in C++? In C++, to concatenate multiple strings means we need to combine two or more strings into a single string. In this article, we will learn how to concatenate multiple strings in C++. Example Input: str1="Hello!" str2="Geek," str3="Happy Coding." Output: Hello! Geek, Happy Coding.Concatenate Multiple 2 min read C++ Program For String to Double Conversion There are situations, where we need to convert textual data into numerical values for various calculations. In this article, we will learn how to convert strings to double in C++. Methods to Convert String to Double We can convert String to Double in C++ using the following methods: Using stod() Fun 3 min read How to Concatenate Multiple C++ Strings on One Line? In C++, strings are used to store a sequence of characters. The combination of multiple strings into a single one is called Concatenation. In this article, we will learn how to concatenate multiple strings in C++. Example Input: str1="Hello!"str2="Geek"str3="Welcome to GfG"Output:Hello! Geek Welcome 1 min read How to Overload the (+) Plus Operator in C++? In C++, operator overloading is a feature of the OOPs concept that allows you to redefine the behavior for different operators when they are used with objects of user-defined classes. The plus operator (+) is a binary operator generally used for addition. In this article, we will learn how to overlo 2 min read How to Overload the Less-Than (<) Operator in C++? In C++ we have an operator called less than operator (<) which checks if the left side operand is smaller than the right side operand or not. In this article, we will learn how to overload the less-than operator in C++. Overloading Less-Than Operator in C++In C++, we can overload the less-than op 2 min read Like