KEMBAR78
Concatenation of two strings using class in c++ | DOCX
/*
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;
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;
}

Concatenation of two strings using class in c++

  • 1.
    /* Program that concatenatestwo 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;
  • 2.
    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; }