下列程序在构造函数和析构函数中申请和释放类的私有成员,请完成该程序。 class Myclas{ int*x; pu
下列程序在构造函数和析构函数中申请和释放类的私有成员,请完成该程序。
class Myclas{
int *x;
public:
MyClas(int n);
~MyClas();
};
MyClas::MyClas(intn){
【 】;
}
MyClas::~MyClas(){
【 】;
}
下列程序在构造函数和析构函数中申请和释放类的私有成员,请完成该程序。
class Myclas{
int *x;
public:
MyClas(int n);
~MyClas();
};
MyClas::MyClas(intn){
【 】;
}
MyClas::~MyClas(){
【 】;
}
下列程序在构造函数和析构函数中申请和释放类的私有成员,请完成该类的实现。
class Myclass {
public:
Myclass(int num);
~ Myclass();
public:
int *p;
};
Myclass: :Myclass(int hum) {
【 】
Myclass:: ~ Myelass() {
【 】;
}。
B、构造函数和析构函数均可以重载
C、已知类Student以及Student*p;在使用p=newStudent;时自动调用无参构造函数创建动态对象,在deleltep;时自动调用析构函数释放动态对象
D、构造函数和析构函数都可以成为虚函数
阅读下列程序说明和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);
}
A.构造函数和析构函数都是成员函数
B.构造函数和析构函数都可以带参数
C.程序中不能直接调用构造函数,但可以调用析构函数
D.构造函数可以重载但析构函数不可以
下列程序的运行结果是 。 class Stack{ char * sp ; public : Stack(){ sp = new char[100] ; strcpy(sp , "I love China") ; cout << "在构造函数中! \n" ; } ~Stack(){ cout << sp << endl ; delete [ ]sp ; cout << "在析构函数中! \n" ; } } ; void main() { Stack s ; }
A.当一个对象生命周期结束时会调用析构函数
B.析构函数没有返回值
C.析构函数的函数名是“~”类名
D.析构函数用于释放对象所占用内存空间
E.与构造是返向过程
A.首先为该动态对象调用构造函数,在释放其占用的内存
B.首先释放该动态对象占用的内存,在为其调用构造函数
C.首先为该动态对象调用析构函数,在释放其占用的内存
D.首先释放动态对象占用的内存,在为其调用析构函数
为了保护您的账号安全,请在“简答题”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!