未來的學校想象作文(精選34篇)
已知類String 的原型為:

class String
{
public:
String(const char *str = NULL); /pic/p>
String(const String &other); /pic/p>
~ String(void); /pic/p>
String & operate =(const String &other); /pic/p>
private:
char *m_data; /pic/p>
};
請編寫String 的上述4 個函數。
標準答案:
/pic/p>
String::~String(void) /pic/p>
{
delete [] m_data;
/pic/p>
}
/pic/p>
String::String(const char *str) /pic/p>
{
if(str==NULL)
{
m_data = new char[1]; /pic/p>
*m_data = ‘\0’;
}
else
{
int length = strlen(str);
m_data = new char[length+1]; /pic/p>
strcpy(m_data, str);
}
}
/pic/p>
String::String(const String &other) /pic/p>
{
int length = strlen(other.m_data);
m_data = new char[length+1]; /pic/p>
strcpy(m_data, other.m_data);
}
/pic/p>
String & String::operate =(const String &other) /pic/p>
{
/pic/pic/p>
if(this == &other)
return *this;
/pic/pic/p>
delete [] m_data;
/pic/pic/p>
int length = strlen(other.m_data);
m_data = new char[length+1]; /pic/p>
strcpy(m_data, other.m_data);
/pic/pic/p>
return *this;
}
【未來的學校想象作文】相關文章:
(經典)未來的學校想象作文04-19
未來的學校小學想象作文07-05
未來的學校想象作文(精選43篇)07-11
未來的學校想象作文(精選36篇)12-18
未來的學校想象作文(精選25篇)09-11
未來的學校小學想象作文(精選29篇)12-24
(實用)未來的學校想象作文11篇06-26
未來的學校想象作文(通用27篇)12-19
初一想象作文:未來學校09-16
未來的學校小學想象作文(通用30篇)12-20