15. Write a program to
concatenate one string to another using binary operator overloading.
/* CONCATENATE ONE STRING TO ANOTHER USING BINARY OPERATOR OVERLOADING */
#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;
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;
}
0 comments:
Post a Comment