Concatenate Two Strings Using Binary Operator Overloading

Introduction

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 the many exciting feature 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. Syntax for operator overloading is below:

  1. <return_type>operator(arg1,agr2,…..,argun)  
  2. {  
  3.     //task for user define  
  4. }  

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 in the class.
  • Define the operator function to implement the required operations.
  • The overloaded operators must have at least one operand that is of user-defined type.
  • We cannot change the basic meaning of an operator. That is, 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:
Operator Description
. Membership operator
.* Pointer to member operator
:: Scope resolution operator
?: Conditional Operator
Sizeof Sizeof 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

Example

This example concatenating two strings using binary operator overloading.

  1. #include<iostream>  
  2. #include<conio.h>  
  3. using namespace std;  
  4.   
  5. class String  
  6. {  
  7.     char str[20]; //member variable for string input  
  8. public:  
  9.     void input() //member function  
  10.     {  
  11.         cout<<"Enter your string: ";  
  12.         cin.getline(str,20);  
  13.     }  
  14.     void display() //member function for output  
  15.     {  
  16.         cout<<"String: "<<str;  
  17.     }  
  18.     String operator+(String s) //overloading   
  19.     {  
  20.         String obj;  
  21.         strcat(str,s.str);  
  22.         strcpy(obj.str,str);  
  23.         return obj;  
  24.     }  
  25. };  
  26. void main()  
  27. {  
  28.     String str1,str2,str3; //creating three object  
  29.     str1.input();  
  30.     str2.input();  
  31.     str3=str1+str2;  
  32.     str3.display(); //displaying  
  33.     getch();  
  34. }   
Concatenate Two String Using Binary Operator Overloading
 

Summary

In this blog, I covered operator overloading, 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.