Concatenation of two strings using class in c++

2

Click here to load reader

Transcript of Concatenation of two strings using class in c++

Page 1: Concatenation of two strings using class in c++

/*

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++

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;

}