# include "iostream"
# include "string"
using namespace std;
class string
{public:
string (char *s)
{ptr=new char[strlen(s)+1];
strcpy(ptr,s);
}
~string()
{delete ptr;
}
void print()
{cout<<ptr<<endl;
}
string& operator=(const string &);
private:
char *ptr;
};
string& string::operator=(const string &b)
{if(this==&b) return *this;
delete ptr;
strcpy(ptr,b.ptr);
ptr=new char[strlen(b.ptr)+1];
return *this;
}
void main()
{string p1("chen");
string p2(" ");
p2=p1;
cout<<"p2:";
p2.print();
cout<<"p1:";
p1.print();
}
# include "string"
using namespace std;
class string
{public:
string (char *s)
{ptr=new char[strlen(s)+1];
strcpy(ptr,s);
}
~string()
{delete ptr;
}
void print()
{cout<<ptr<<endl;
}
string& operator=(const string &);
private:
char *ptr;
};
string& string::operator=(const string &b)
{if(this==&b) return *this;
delete ptr;
strcpy(ptr,b.ptr);
ptr=new char[strlen(b.ptr)+1];
return *this;
}
void main()
{string p1("chen");
string p2(" ");
p2=p1;
cout<<"p2:";
p2.print();
cout<<"p1:";
p1.print();
}