国家二级C++机试(操作题)模拟试卷377
基本操作题
1.请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程pmjl,此工程中包含了类Pets(“宠物”)和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
Name:sonny Type:dog
Name:John Type:dog
Name:Danny Type:eat
Name:John Type:dog
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include
using namespace st:d;
enllm Pets type{dog,cat,bird,fish};
class PetS{
private:
char*name;
Pets_type type;
public:
Pets(const char*flame=”sonny”,
Pets_type type=dog);
Pets&operator=(const Pets&s);
~Pets();
void show()const;
};
Pets::Pets(const char*name,Pets
type type)
//构造函数
{
this->name=new char[strlen
(name)+1];
strcpy(this一>name,name);
//ERROR*********found*********
type=type;
}
Pets::~Pets()//析构函数,释放name
所指向的字符串
{
//ERROR*********found*********
name=’/0’;
}
Pets&Pets::operator=(const Pets
&s)
{
if(&s=this)//确保不要向自身赋值
return*this;
delete[]name;
name=new char[strlen(S.name)+
1];
//ERROR*********found*********
strcpy(this一>name,name);
type=S.type;
return*thiS ;
}
void Pets::show()const
{
cout<<”Name:”<<name<<”Type:”;
switch(type)
{
case dog:cout<<”dog”;break;
case cat:cout<<”cat”;break;
case bird:cout<<”bird”;break;
case fish:cout<<”fish”;break;
}
cout<<endl;
}
int main()
{
Pets mypet1,mypet2(”John”,dog);
Pets youpet(”Danny”,cat);
mypet1.show(),
mypet2.shOW();
youpet.show();
youpet=mypet2;
youpet.show();
return 0;
}
(1)this->type=type;
(2)delete[]nal/le;
(3)strepy(this->nanle,s.name);
解析:(1)主要考查考生对构造函数的掌握情况,因为形参名和类的私有成员名称都是type,为了避免混淆,所以规定类的私有成员使用this指针调用,即:this一>type=type;。
(2)主要考查考生对析构函数的掌握情况,题目中要求,释放name所指向的字符串。要释放name指针用delete语句,即delete[]name;。
(3)主要考查考生对strepy函数的掌握情况,strcpy函数的形参为两个字符串,而name为指向字符串的指针,因此使用语句:strcpy(this一>nallle,s.name);.
简单应用题
2.使用VC6打开考生文件夹proj2下的工程proj2,其中有两个类:一是销售类(Sale),用于表示按照一件商品的基本价格进行销售;另一个是打折销售类(DiscountSale),用于表示在基本价格基础上按一个折扣比例进行销售。DiscountSale类继承了sale类。类的主要数据成员的含义和成员函数的功能要求在程序注释中有说明。请在程序中的横线处填写适当的代码,然后删除横线,完成程序的功能。此程序的正确输出结果应为:
Discount item is cheaper.
Saving is 0.1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。
#include
using namespace std;
class Sale
{
public:
Sale();//默认构造函数,将price初始化为0
Sale(double the_price);//构造函数,用the_price初始化price
virtual double bill()const;//返回当前商品的价格(基本价)
double savings(const Sale&other)const;//返回参数other所引用的对象比当前对象便宜的差价
protected:
double price;//商品的基本价格(不打折的价格)
};
Sale::Sale():price(0){)
Sale::Sale(double the_price):price
(the_price){)
double Sale::bill()const
{
return price;
}
double Sale::savings(const.Sale
&other)const
{
//ERROR******found******
_________;//返回当前对象价格
本文档预览:3600字符,共8068字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载