Program to overload ‘+’ operator for concating the strings
#include<iostream.h> #include<string.h> class string{ int len; char *name; public: string() { len = 0; name = new char[1]; } string(char *s) { len = strlen(s); name = new char[len+1]; strcpy(name,s); } void display() { cout<<name<<endl; } friend string operator +(string t, string s); }; string operator +(string t, string s) { string temp; temp.len = t.len+s.len; temp.name = new.char[temp.len+1]; strcpy(temp.name t.name); strcat(temp.name, s.name); return temp; } int main() { char a[10],b[10]; cout<<"Enter the string S1:"; cin>>a; string s1(a); cout<<"enter the string S2: "; cin>>b; string s2(b); string s3=s1+s2; cout<<"\n ......OUTPUT......\n"; cout<<"\n S1= "; s1.display(); cout<<"\n S2= "; s2.display(); cout<<"\n S1+S2= "; s3.display(); return(0); }
[wp_ad_camp_1]
OUTPUT Enter the string S1: new Enter the string S1: york ......OUTPUT...... S1 = new S2 = york S1+S2= newyork