2018年9月国家二级C++机试(操作题)真题试卷汇编1
基本操作题
1.请打开考生文件夹下的解决方案文件proj1,其中定义了一个CD类。程序中位于每个//ERROR**********found**********下的语句行有错误,请加以更正,不得修改程序的其他部分。更正后程序的输出应该是:
歌唱祖国30
义勇军进行曲95
注意:只能修改每个//ERROR**********found**********下的那一行,不要改动程序中的其他内容。
#include
#include
using namespace std;
class CD
{
char name[20];
int number;
public:
void init(char*aa,int
bb)
{
//ERROR**********found**********
name=aa;
number=bb;
}
char * getName(){
//ERROR *****found*****
return*name;
}
int getNumber()(return
number;}
void output(){
//ERROR *****found*****
cout<<name[20]<<’’
<<number<<endl;
}
};
void main()
{
CD dx,dy;
dx.init(\\
(1)strcpy(name,aa);
(2)return name;
(3)cout<<Raffle<<’’<<number<<endl:
解析:程序定义CD类,它包含两个数据成员:字符数组name和整型变量number;还包含四个公有成员函数,init()函数接收两个参数,用参数对数据成员进行赋值;getName()函数返回数据成员name;getNumber()函数返回数据成员number;output()函数将数据成员name和number输出。CD类的定义体中,有三个错误:
(1)init()函数将形参aa赋给name,由于name是字符数组,所以不能通过简单的赋值运算符进行赋值,应该使用strcpy()函数将形参aa指向的字符串拷贝到name中。
(2)getName()函数的返回值为char*类型,所以函数体的return语句应该返回name,而不是name指向的字符串。
(3)output()函数需要输出两个数据成员,输出字符数组时,只需要给出数组名name即可。
简单应用题
2.请打开考生文件夹下的解决方案文件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******
____________;//返回当前对
象价格比other贵多少的差价
}
class DiscountSale:public
Sale//打折销售类继承销售类
{
public:
DiscountSale(),//默认构造函数,将discount初始化为0
DiscountSale(double the_price,double the_discount);//构造函数,the_price是基本价格;the_discount是折扣百分比
virtual double bill()const;//返回本商品销售价格(即打折以后的实际售价,覆盖了基类的biii函数)
protected:
double discount;//折扣百分比。例如降价至原价的70%,此成员值应为70
};
DiscountSale::DiscountSale():
discount(0){}
DiscountSale::DiscountSale
(double the_price,double the_
discount)
:Sale(the price),discount
(the discount){}
double DiscountSale::bill ( )
const
{
double fraction=discount/
100;
// ******found******
___________;//返回本对象打折以后的实际售价
}
bool operator<(const Sale&
first,const Sale&second)
{
// ******found******
___________;//判断是否
first价格低于second价格
}
in
本文档预览:3600字符,共9767字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载