Write a program of string manipulation using operator overloading


 

/* Write a program of  string manipulation using operator overloading*/

#include<iostream.h>

#include<conio.h>

#include<string.h>

class string

{

char st[20];

public:

string()

{

strcpy(st,” “);

}

string(char s[])

{

strcpy(st,s);

}

void show()

{

cout<<st;

}

string operator+(string);

};

string string::operator+(string s2)

{

string temp;

strcpy(temp.st,st);

strcat(temp.st,s2.st);

return temp;

}

void main()

{

clrscr();

string ss1=”HAPPY”;

string ss2=”BIRTHDAY”;

string ss3;

cout<<“before concat=”;

cout<<“\n”<<“ss1=”;

ss1.show();

cout<<“\n”<<“ss2=”;

ss2.show();

ss3.show();

ss3=ss1+ss2;

cout<<“\n”<<“after concat=”;

cout<<“\n”<<“ss3=”;

ss3.show();

getch();

}

/* output:

before concat=

ss1=HAPPY

ss2=BIRTHDAY

after concat=

ss3=HAPPYBIRTHDAY*/

Related Posts

Comments are closed.