Combining strings involves merging two separate strings to create a unified string by adding the characters of the second string to the end of the first string. This process results in a single concatenated string.
Example:
Input1: st1="Over",st2="loading"
Output: Overloading
Input1: st1="Left",st2="Join"
Output: LeftJoin
Approach 1: By using the Unary Operator Overloading
- Unary operator overloading is used to concatenate two strings. Create a class that has two string parameters.
- Please create a new instance of the class and use its parameterized constructor to initialize the two string parameters with the main function's input values.
- Use the unary operator + to combine these two string parameters for a class instance.
- Finally, use the operator method to join two class variables.
The method described above is executed in the following manner:
Example:
Filename: OperatorOverloading.cpp
//Concatenation of two strings in C++ by using the unary operator overloading
#include <iostream>
#include <string.h>
using namespace std;
//// method for string concatenation // class to implement overloading of operators
class AddingString {
public:
// the string objects declarations
char st1[25], st2[25];
//the parameterized constructor
AddingString(char string1[], char string2[])
{
// the initialization of the string objects
strcpy(this->st1, string1);
strcpy(this->st2, string2);
}
//the operator for overloading
void operator+()
{
cout << "\nThe Concatenation form of the string: " << strcat(st1, st2);
}
};
int main()
{
// the string declaration
char string1[] = "Operator";
char string2[] = "Overloading";
// Declaring and setting up the class // using the two strings mentioned above
AddingString op(string1, string2);
// operator calling function
+op;
return 0;
}
Output:
The Concatenation form of the string: OperatorOverloading
Explanation:
- In this illustration, a class named AddingString is employed to combine strings. It preserves the input strings in st1 and st2 member variables. The constructor AddingString initializes st1 and st2 with the provided strings. The operator "+ " has been redefined to utilize strcat for string concatenation and displaying the outcome.
- The main function initializes two strings, string1 and string2. By utilizing these strings, it creates an instance op of the AddingString class. The concatenation process is then initiated, and the output is exhibited by invoking the operator "+" method with the unary "+" operator on the op object.
Approach 2: By using binary operator overloading
Define a class containing a string variable, along with an operator method named "+" that accepts another instance of the class as an argument and merges the string variable of that instance with the current instance's string variable.
Create two distinct objects of the class and set each object's class variable with different input strings during initialization.
The class variable within each instance must now be combined using the overloading operator function (+).
Example:
Filename:Binary.cpp
// A C++ program that uses binary operator overloading for combining two strings
#include <iostream>
#include <string.h>
using namespace std;
// A class that implements the operator overloading method // to join strings together
class AddingString {
public:
// the class having the string object
char str[100];
// no parameters have passed
AddingString() {}
//The constructor having the parameters
AddingString(char strs[])
{
strcpy(this->str, strs);
}
// the operator + is overloaded
AddingString operator+(AddingString& st2)
{
// the copy of the constructor
AddingString st3;
// by using the inbuilt method to add
strcat(this->str, st2.str);
// copying of the strin
strcpy(st3.str, this->str);
// an objecct is returned
return st3;
}
};
// Driver Code
int main()
{
//two string variables declaration
char strs1[] = "Programming";
char strs2[] = "Subjects";
// a class declared and also initialized in the AddingString class
AddingString st1(strs1);
AddingString st2(strs2);
AddingString st3;
// the operator is calling
st3 = st1 + st2;
cout << "The Concatenated form of the string is: " << st3.str;
return 0;
}
Output:
The Concatenated form of the string is: ProgrammingSubjects