#include <iostream>#include<string.h>using namespacestd;classstring{Char*m_data; Public: String (Const Char* p =NULL) { if(p = =NULL) {m_data=New Char[1]; *m_data =' /'; } Else{m_data=New Char[Strlen (P) +1]; strcpy (M_data, p); }} String (ConstString &Other ) { if(&other! = This) { //Delete [] m_data;//¹¹ô캯êýöð²»ðèòªdelete momory:wM_data =New Char[Strlen (Other.m_data) +1]; strcpy (M_data, other.m_data); } } ~String () {Delete[] m_data; } String&operator=(ConstString &Other ) { if(&other! = This) { Delete[] m_data; M_data=New Char[Strlen (Other.m_data) +1]; strcpy (M_data, other.m_data); } return* This; } Friend Stringoperator+(ConstString &s1,ConstString &S2); Friend Inline Ostream&operator<< (ostream & OS, String &str);}; Stringoperator+(ConstString &s1,ConstString &12) {String temp; Delete[] temp.m_data;//Temp.dataêç½öº¬¡®\0¡¯µäxö û´®Temp.m_data =New Char[Strlen (S1.m_data) + strlen (s2.m_data) +1]; strcpy (Temp.m_data, s1.m_data); strcat (Temp.m_data, s2.m_data); returntemp;} Ostream&operator<< (ostream & OS, String &str) {OS<< Str.m_data <<Endl; returnos;}intMain () {String str1 ("ABC"); cout<<str1; String str2 (STR1); cout<<str2; String STR3; cout<<str2; STR3=str2; String STR4 ("def"); String STR5; STR5= Str3 +STR4; cout<<STR5;}
Where to look at C + + class from the String class