二、基本操作題
41清使用VC6或使用【答題】菜單打開考生文件夾projl下的工程projl,此工程中包含了類Pets(“寵物”) 和主函數(shù)main的定義。程序中位于每個(gè)“//ERROR****found ****”之后的一行語句有錯(cuò)誤,請(qǐng)加以改 正。改正后程序的輸出結(jié)果應(yīng)為:
Name:sonny Type:dog
Name:John Type:dog
Name:Danny Typc:cat
Name:John Type:dog
注意:只修改每個(gè)“//ERROR ****found ****”下的那一行,不要改動(dòng)程序中的其他內(nèi)容。
#include
using namespace sm; enum Pets_type{d09,cat,bird,fish};
class Pets{ private:
char *name; Pets_type type; public:
Pets(const char *name=”sonny”,Pets_type type=dog);
Pets&operator=(const Pets&s);
~Pets();
void show()eonst;};
Pets::Pets(eonst char$naIne,Pets_type type)
//構(gòu)造函數(shù) {
This ->name=new char[strlen(name)+1];
strcpy(this一>name,name);
//ERROR *********found********* type=type;
}{ Pets::~Pets()//析構(gòu)函數(shù),釋放name所指向的字符串
{ //ERROR *********found********* name=’/0‘;
}
Pets&Pets::0perator=(const Pets&s){
if(&s==this)//確保不要向自身賦值 return *this;
delete[]name;
name=new char[strlen(S.name)+1];//ERROR *********found*********
strcpy(this一>nmne,name); type=S.type:
return *this;}
void Pets::show()const
cout<<“Name:”<
參考解析:
(1)this一>type=type;
(2)delete[]name;
(3)strepy(this->name,s.name);
41清使用VC6或使用【答題】菜單打開考生文件夾projl下的工程projl,此工程中包含了類Pets(“寵物”) 和主函數(shù)main的定義。程序中位于每個(gè)“//ERROR****found ****”之后的一行語句有錯(cuò)誤,請(qǐng)加以改 正。改正后程序的輸出結(jié)果應(yīng)為:
Name:sonny Type:dog
Name:John Type:dog
Name:Danny Typc:cat
Name:John Type:dog
注意:只修改每個(gè)“//ERROR ****found ****”下的那一行,不要改動(dòng)程序中的其他內(nèi)容。
#include
using namespace sm; enum Pets_type{d09,cat,bird,fish};
class Pets{ private:
char *name; Pets_type type; public:
Pets(const char *name=”sonny”,Pets_type type=dog);
Pets&operator=(const Pets&s);
~Pets();
void show()eonst;};
Pets::Pets(eonst char$naIne,Pets_type type)
//構(gòu)造函數(shù) {
This ->name=new char[strlen(name)+1];
strcpy(this一>name,name);
//ERROR *********found********* type=type;
}{ Pets::~Pets()//析構(gòu)函數(shù),釋放name所指向的字符串
{ //ERROR *********found********* name=’/0‘;
}
Pets&Pets::0perator=(const Pets&s){
if(&s==this)//確保不要向自身賦值 return *this;
delete[]name;
name=new char[strlen(S.name)+1];//ERROR *********found*********
strcpy(this一>nmne,name); type=S.type:
return *this;}
void Pets::show()const
cout<<“Name:”<
參考解析:
(1)this一>type=type;
(2)delete[]name;
(3)strepy(this->name,s.name);