concatenation of two strings using class in c++
TRANSCRIPT
![Page 1: Concatenation of two strings using class in c++](https://reader038.vdocuments.us/reader038/viewer/2022100803/5a64f2337f8b9aa6218b4661/html5/thumbnails/1.jpg)
/*
Program that concatenates two strings defined as private members
in a class using friend function
*/
#include<iostream>
#include<string.h>
using namespace std;
class str
{
char *st;
public:
str(int len, char *s)
{
int l=strlen(s);
st= new char[l];
st=s;
}
str()
{
}
void put();
str operator +(str);
};
str str :: operator +(str s)
{ /*str abc;
![Page 2: Concatenation of two strings using class in c++](https://reader038.vdocuments.us/reader038/viewer/2022100803/5a64f2337f8b9aa6218b4661/html5/thumbnails/2.jpg)
abc.st=strcat(st, s.st);
return abc;*/
st=strcat(st, s.st);
return *this;
}
void str :: put()
{
cout<<"\n The string after concatenation is : \n"<<st;
}
int main()
{
char str1[30], str2[30];
cout<<"\n Enter a string \n";
cin>>str1;
cout<<"\n Enter the 2nd string \n";
cin>>str2;
int l1= strlen(str1), l2=strlen(str2);
str s2(l1,str1), s3(l2,str2), s4;
s4=s2+s3;
s4.put();
return 0;
}