编写类 String 的构造函数、析构函数和赋值函数 已知类 String的原型为: class String { pu

题目

编写类 String 的构造函数、析构函数和赋值函数

已知类 String的原型为:

class String

{

public:

String(const char *str = NULL); // 普通构造函数

String(const String &other); // 拷贝构造函数

~ String(void); // 析构函数

String & perate =(const String &other); // 赋值函数

private:

char *m_data; // 用于保存字符串

};

请编写 String的上述 4 个函数。

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

关于构造函数和析构函数的说法,正确的是()

A.构造函数和析构函数默认的返回类型是void

B.构造函数和析构函数都可以重载

C.在类中未定义任何构造和析构函数时,系统会自动生成默认构造函数和析构函数。

D.构造函数和析构函数都可以有参数


构造函数可以有多个

第2题:

对类的构造函数和析构函数描述正确的是

A.构造函数可以重载,析构函数不能重载
B.构造函数不能重载,析构函数可以重载
C.构造函数可以重载,析构函数也可以重载
D.构造函数不能重载,析构函数也不能重载

答案:A
解析:

第3题:

对类的构造函数和析构函数描述正确的是( )。

A.构造函数可以重载,析构函数不能重载

B.构造函数不能重载,析构函数可以重载

C.构造函数可以重载,析构函数也可以重载

D.构造函数不能重载,析构函数也不能重载


正确答案:A

第4题:

已知类 String 的原型为

class string

{

public:

string(const char *str=null);//普通构造函数

string(const string &other);//拷贝构造函数

---string(void);

string &operate=(const string &other);//赋值函数

private:

char * m-data;//用于保存字符串

};

请编写 string 的上述4 个函数


正确答案:
 

第5题:

编写类 String 的构造函数,析构函数和赋值函数


正确答案:
 

第6题:

已知String类定义如下:

class String

{

public:

String(const char *str = NULL); // 通用构造函数

String(const String &another); // 拷贝构造函数

~ String(); // 析构函数

String & perater =(const String &rhs); // 赋值函数

private:

char *m_data; // 用于保存字符串

};

尝试写出类的成员函数实现。


正确答案:

 

String::String(const char *str)
{
if ( str == NULL ) //strlen在参数为NULL时会抛
异常才会有这步判断
{
m_data = new char[1] ;
m_data[0] = '\0' ;
}
else
{
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}
}
String::String(const String &another)
{
m_data = new char[strlen(another.m_data) + 1];
strcpy(m_data,other.m_data);
}
String& String::operator =(const String &rhs)
{
if ( this == &rhs)
return *this ;
delete []m_data; //删除原来的数据,新开一块内

m_data = new char[strlen(rhs.m_data) + 1];
strcpy(m_data,rhs.m_data);
return *this ;
}
String::~String()
{
delete []m_data ;
}

第7题:

阅读下列程序说明和C++程序,把应填入其中(n)处的字句,写对应栏内。

【说明】

下面的程序实现了类String的构造函数、析构函数和赋值函数。

已知类String的原型为:

class String

{

public:

String(coust char * str = NULL); //普通构造函数

String( const String &other); //拷贝构造函数

~String(void); //析构函数

String & perate =(const String &other); //赋值函数

private:

char * m_data; // 用于保存字符串

};

//String 的析构函数

String:: ~String (void)

{

(1);

}

//String 的普通构造函数

String: :String( const char * str)

{

if (2)

{

m_data = new char[1];

*m_data = '\0';

}

else

{

int length = strlen(str);

m_data = new ehar[ length + 1 ];

strepy(m_data, str);

}

}

//拷贝的构造函数

String:: String( const String &other)

{ int length = strlen(other. m_data);

m_data = new char[ length + 1 ];

strepy(m_data, other, m_data); //赋值函数

String & String::operate = (eonst String &other) //

{

if (3)

return * this;

delete [] m_clara; //释放原有的内存资源

int length = strlen( other, m_data);

m_data = new chart length + 1 ];

(4);

return (5);

}


正确答案:(1)delete[]m_data或者delere m_data
(1)delete[]m_data或者delere m_data 解析:由于m_data 是内部数据类型,也可以写成delete m_data。

第8题:

对类的构造函数和析构函数描述正确的是( )。

A.构造函数可以重载,析构函数不能重载

B.构造函数不能重载,析构函数可以重载

C.构造函数可以重载,析构函数也可以重载

D.构造函数不能重载,析构函数也不能重裁


正确答案:A
一个类中只能定义一个析构函数,否则会造成对同…对象的多次删除;而构造函数可以根据不同的参数个数和类型进行多次重载。

第9题:

对类的构造函数和析构函数描述正确的是

A.构造函数可以重载,析构函数不能重载

B.构造函数不能重载,析构函数可以重载

C.构造函数可以重载,析构函数也可以重载

D.构造函数不能重载,析构函数也不能重载


正确答案:A
解析:一个类中只能定义一个析构函数,否则会造成对同一对象的多次删除;而构造函数可以根据不同的参数个数和类型进行多次重载。