C++ program to concatenate two strings using operator overloading
Operators can be customized to operate with user-defined types because of operator overloading. Concatenation is a frequently performed string manipulation operation that can be accomplished by overloading the + operator for custom string classes. An example of this can be found in the C++ program that follows. It defines a class called MyString that has a constructor for initializing strings and a destructor for memory management. Combining strings is now easier to understand and more readable due to the overloading of the + operator, which concatenates two instances of the MyString class. The software creates two MyString objects, each of which represents a sentence fragment, concatenates them using the overloaded + operator, and displays the outcome to illustrate this functionality.
Example:
Input: str1 = "hello", str2 = "world"
Output: helloworld
Input: str1 = "Java", str2 = "tpoint"
Output: Javatpoint
Using Unary Operator Overloading:
Unary operator overloading is used to concatenate two strings.
- Declare two string variables in your class.
- Create an instance of the class and call its parameterized constructor 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 a class instance.
- Ultimately, invoke the operator function and join two class variables together.
Example:
Let's take an example to illustrate theconcatenation of two strings using the unary operator overloading function in C++.
#include <iostream>
#include <cstring>
class MyString {
private:
char *str;
public:
// Constructor
MyString(const char *s = "") {
str = new char[strlen(s) + 1];
strcpy(str, s);
}
// Destructor
~MyString() {
delete[] str;
}
// Overloading + operator for string concatenation
MyString operator+(const MyString &other) const {
MyString result;
result.str = new char[strlen(str) + strlen(other.str) + 1];
strcpy(result.str, str);
strcat(result.str, other.str);
return result;
}
// Display the string
void display() const {
std::cout << str << std::endl;
}
};
int main()
{
// Create two MyString objects
MyString str1 = "Hello, ";
MyString str2 = "world!";
// Concatenate strings using operator overloading
MyString result = str1 + str2;
// Display the concatenated string
std::cout << "Concatenated String: ";
result.display();
return 0;
}
Output:
Concatenated String: Hello, world!
Example 2:
Let's take an example to illustrate theconcatenation of two strings using the operator overloading function in C++.
#include <iostream>
#include <string>
class ConcatenatedString {
private:
std::string str;
public:
ConcatenatedString(const std::string& s = "") : str(s) {}
// Overloading + operator for string concatenation
ConcatenatedString operator+(const ConcatenatedString& other) const {
ConcatenatedString result;
result.str = this->str + other.str;
return result;
}
// Display the string
void display() const {
std::cout << str << std::endl;
}
};
int main() {
// Create two ConcatenatedString objects
ConcatenatedString str1("Java ");
ConcatenatedString str2("tpoint");
// Concatenate strings using operator overloading
ConcatenatedString result = str1 + str2;
// Display the concatenated string
std::cout << "Concatenated String: ";
result.display();
return 0;
}
Output:
Concatenated String: Java tpoint
Example 3:
Let's take another example to illustrate theconcatenation of two strings using the operator overloading function in C++.
#include <iostream>
#include <cstring>
class CustomString {
private:
char *str;
public:
// Constructor
CustomString(const char *s = "") {
str = new char[strlen(s) + 1];
strcpy(str, s);
}
// Destructor
~CustomString() {
delete[] str;
}
// Overloading + operator for string concatenation
CustomString operator+(const CustomString &other) const {
CustomString result;
result.str = new char[strlen(str) + strlen(other.str) + 1];
strcpy(result.str, str);
strcat(result.str, other.str);
return result;
}
// Display the string
void display() const {
std::cout << str << std::endl;
}
};
int main() {
// Create two CustomString objects
CustomString str1 = "Greetings, ";
CustomString str2 = "world!";
// Concatenate strings using operator overloading
CustomString result = str1 + str2;
// Display the concatenated string
std::cout << "Concatenated String: ";
result.display();
return 0;
}
Output:
Concatenated String: Greetings, world!
Example 4:
Let's take an example to illustrate theconcatenation of two strings using the binary operator overloading function in C++.
#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[] = "Hello, ";
char str2[] = "everyone";
// 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: Hello, everyone
Conclusion:
This language feature offers versatility and flexibility, as demonstrated by the C++ program that concatenates two strings using operator overloading. The software accomplishes a natural and expressive method of concatenating strings by overloading the + operator inside a custom class. Using operator overloading, the example shows how to create an AddString class and combine two strings in an elegant manner. Besides using the strcpy and strcat functions for effective string manipulation, the program also includes dynamic memory allocation. In addition to improving code readability, this method encourages a modular and object-oriented design by encapsulating the concatenation logic inside the class.