Converting Base Type to Class Type (char to string)
class String
{ char *name;
int length;
public:
String(void)
{length =0;
name = new char[length+1];
}
String(char *s)
{length = strlen(s);
name = new char[length+1];//for null terminator strcpy(name,s);
}
void display(void)
{cout<
}
void join(String &a, String &b);
friend String operator+(String &a, String &b);
};
String operator+(String &a, String &b)
{String temp;
delete temp.name;
temp.length = a.length+b.length; temp.name = new char[temp.length+1]; strcpy(temp.name,a.name); strcat(temp.name,b.name);
return temp;
}
void String::join(String &a, String &b)
{length = a.length+b.length;
delete name;
name = new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
};
void main()
{
char *first="Electronic ";
String name1(first); String name2("and Communication ");String name3("Engineering");
String s1,s2; s1.join(name1,name2); s2.join(s1,name3); s1=name1+name2; s2=s1+name3;
name1.display();name2.display();name3.display();
s1.display();
s2.display();
}