C++ Program To Concatenate Two Strings Using Operator Overloading

String concatenation refers to the collection of characters that join two additional strings to produce a concatenated single string. While concatenating the strings, the second string is appended to the end of the first string to form a single string.

Example:

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 previously mentioned technique is implemented as follows:

Example:

Filename: OperatorOverloading.cpp

Example

//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:

Output

The Concatenation form of the string: OperatorOverloading

Explanation:

  • In this example, a class called AddingString is used to concatenate strings. It stores the input strings in two member variables, st1 and st2 . The supplied strings are used by the constructor AddingString for initializing st1 and st2. The operator "+ "function has been overloaded to use strcat to conduct string concatenation and show the result.
  • Two strings, string1, and string2, are initialized by the main function. Using string1 and string2, it generates an object op of the AddingString The concatenation is then started, and the result is printed by calling the operator "+" function with the unary "+" operator using the op object.
  • Approach 2: By using binary operator overloading

Create a class that has a string variable and an operator function called "+" that takes an instance of the class's members as input and combines the variable of that instance with the present instance's string variable.

Make two separate instances of the class and initialize both of the input strings in each instance's class variable.

The class variable of each of the instances should now be concatenated using the overloading operator (+) function.

Example:

Filename:Binary.cpp

Example

// 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:

Output

The Concatenated form of the string is: ProgrammingSubjects

Input Required

This code uses input(). Please provide values below: