FY-4-c Easy C++ program to overload the operator for concatenating two strings

C++ program to overload the operator for concatenating two strings

Overload the + for concatenating the two strings. For e.g “Py” + “thon” = Python

Click here to get other OOP Practical

FYIT practical 4-c Unary Operator Overloading

#include<iostream>
#include<string.h>
using namespace std;
class Concate
{
char s[100];
public:
void get()
{
cin>>s;
}
void disp()
{
cout<<endl<<s;
}
Concate operator +(Concate s1)
{
Concate s2;
strcpy(s2.s,s);
strcat(s2.s,s1.s);
return s2;
}
};
int main()
{
Concate s1,s2,s3;
cout<<endl<<"Enter first string value : ";
s1.get();
cout<<endl<<"Enter second string value : ";
s2.get();
s1.disp();
s2.disp();
s3=s1+s2;
cout<<endl<<"After concatenation : ";
s3.disp();
return 0;
}

 

Output

concatenating two strings itvoyagers

Check out more FYIT OOP Practical Program and other logic building programs in C++

We are aiming to explain all concepts of C++ and OOP in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment