Skip to content
Related Articles

Related Articles

Improve Article
C++ Program to concatenate two strings using Operator Overloading
  • Difficulty Level : Medium
  • Last Updated : 17 May, 2021

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

 

Attention reader! Don’t stop learning now. Learn all GATE CS concepts with Free Live Classes on our youtube channel.




My Personal Notes arrow_drop_up
Recommended Articles
Page :