Question:7
Class string(str)
Declare a class named String with the attribute as str. Take input for two Strings. Concatenate (Join) and display them.(Without using friend function).
Sol:
#include<iostream> #include<string.h> using namespace std; class string1 { private: char str[20]; public: void read(); void show(); string1 add(string1 d); }; void string1::read() { cout<<"Enter any string "; cin>>str; } void string1::show() { cout<<"Str is "<<str<<endl; } string1 string1::add(string1 d) { string1 t; strcpy(t.str,str); strcat(t.str,d.str); return(t); } int main() { string1 e1,e2,e3; e1.read(); e2.read(); e1.show(); e2.show(); e3=e1.add(e2); e3.show(); return(0); }
Output:
Enter any string Hello
Enter any string World
Str is Hello
Str is World
Str is HelloWorld