Download to read offline
![/*
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;](https://image.slidesharecdn.com/concatinateclass1-170514083317/75/Concatenation-of-two-strings-using-class-in-c-1-2048.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;
}](https://image.slidesharecdn.com/concatinateclass1-170514083317/75/Concatenation-of-two-strings-using-class-in-c-2-2048.jpg)

This C++ program defines a class called "str" that represents strings as private character array members. It overloads the + operator as a friend function to concatenate two "str" objects by calling strcat() on their character arrays and returning a new "str" object. The main function demonstrates creating "str" objects from input strings, concatenating two of them by calling the overloaded + operator, and printing out the result.
![/*
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;](https://image.slidesharecdn.com/concatinateclass1-170514083317/75/Concatenation-of-two-strings-using-class-in-c-1-2048.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;
}](https://image.slidesharecdn.com/concatinateclass1-170514083317/75/Concatenation-of-two-strings-using-class-in-c-2-2048.jpg)