Tuesday, 8 October 2024

Build a class String. Use overloaded operator to combine two string.

 /*

Definition : Build a class String. Use overloaded operator + to

combine two string.

*/

#include<iostream.h>

#include<conio.h>

#include<string.h>

class String

{

char str[100];

public:

void getData()

{

cout<<"\n Enter String : ";

cin>>str;

}

void operator +(String s2)

{

int i,len1,len2;

len1=strlen(str);

len2=strlen(s2.str);


for(i=0;i<len2;i++)

{

str[len1]=s2.str[i];

len1=len1+1;

}

str[len1]='\0';

}

void putdata()

{

cout<<"\n Concated String is : "<<str;

}

};

int main()

{

clrscr();

String s1,s2;

s1.getData();

s2.getData();

s1+s2; //s2+s1; or s1.operator +(s2);

s1.putdata();

getch();

return 0;

}

No comments:

Post a Comment