Concatenate two strings using binary operator overloading in C++

In this blog, we will learn about operator overloading and see one example that concatenates two strings using binary operator overloading. Let’s start.

Operator Overloading

Operator overloading is one of the many exciting features of the C++ language. It provides a special meaning to an operator. The insertion (<<) and extraction (>>) operator is the best example of operator overloading.

Operator overloading defines a different meaning to an operator, and the operator function is used. The syntax for operator overloading is below:

    //task for user define  

Rules for Operator Overloading

There are certain restrictions and limitations in operator overloading. Some of them are listed below:

  • Declare the operator function in the public section of the class.
  • Define the operator function to implement the required operations.
  • The overloaded operators must have at least one operand of the user-defined type.
  • We cannot change the basic meaning of an operator. We cannot redefine the plus (+) operator to subtract one value from the other.
  • Binary arithmetic operators such as +,-,*, and / must explicitly return a value.
  • There are some operators that cannot be overloaded, and they are listed below:
.Membership operator
.*Pointer to member operator
::Scope resolution operator
?:Conditional Operator
SizeofSizeof operator

Binary Operator Overloading

A binary operator is an operator that operates on two operands. For example, the plus “+” operator is a binary operator since it operates on two operands as in:

C = A + B


This example concatenates two strings using binary operator overloading.

using namespace std;  
class String  
    char str[20]; //member variable for string input  
    void input() //member function  
        cout<<"Enter your string: ";  
    void display() //member function for output  
        cout<<"String: "<<str;  
    String operator+(String s) //overloading   
        String obj;  
        return obj;  
void main()  
    String str1,str2,str3; //creating three object  
    str3.display(); //displaying  


In this blog, I covered operator overloading, and some restrictions for operator overloading, and saw an example that concatenates two strings using binary operator overloading in the C++ language. If you face any problems please comment. Thanks for reading.

Leave a Reply

Your email address will not be published.