Answer to Question #193720 in C++ for Kaviyan P

Question #193720

Write an overloaded function to concatenate two strings using + operator overloading.


1
Expert's answer
2021-05-16T14:40:05-0400
#include<bits/stdc++.h>
using namespace std;
class StringConcatenate
{
public:
    char s1[40];
    StringConcatenate(){}
    StringConcatenate(char s1[])
    {
        strcpy(this->s1,s1);
    }
    StringConcatenate operator+(StringConcatenate& s2)
    {
        StringConcatenate s3;
        strcat(this->s1,s2.s1);
        strcpy(s3.s1,this->s1);
        return s3;
    }
};
int main()
{
    char c1[40],c2[40];
    cout<<"Enter first string : ";
    cin.getline(c1,40);
    cout<<"Enter second string : ";
    cin.getline(c2,40);
    StringConcatenate a1(c1),a2(c2),a3;
    a3=a1+a2;
    cout<<"\nConcatenated String : "<<a3.s1;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog