C++ Program to concatenate two strings using Operator Overloading

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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];
  
    // Parametrized 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;
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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() {}
  
    // Parametrized 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;
}

chevron_right


Output:

Concatenation: GeeksForGeeks


My Personal Notes arrow_drop_up

सर्वशक्तिशाली इकलौता

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.