国家二级C++机试(操作题)模拟试卷500
基本操作题
1.请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
value=63
number=1
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include
using namespace std;
class MyClass{
int*p;
const int N;
public:
//ERROR ********found********
MyClass(int val):N=1
{
p=new int;
*p=val;
}
//ERROR********found********
~MyClass(){delete*p;)
friend void print(MyClass&obj);
};
//ERROR********found********
void MyClass::print (MyClass&obj)
{
cout<<\\
(1)MyClass(int val):N(1)
(2)~MyClass(){delete[]p;}
(3)void print(MyClass&obj)
解析:(1)主要考查考生对构造函数的掌握,在这里不能使用赋值语句。
(2)主要考查考生对析构函数的掌握,析构函数的delete语句要使用标识符“[]”,即delete[]p;。
(3)主要考查考生对友元函数的掌握,友元函数并不属于类,因此定义时前面不用加类名和作用域符号。
简单应用题
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*****
_________;//返回当前对象价格比other贵多少的差价
}
class DiscountSale:public
Sale//打折销售类继承销售类
{
public:
DiscountSale();//默认构造函数,将discount初始化为0
DiscountSale(double the_price,double the discount);//构造函数,the_price是基本价格;the discount是折扣百分比
virtual double bill()const;//返回本商品销售价格(即打折以后的实际售价,覆盖了基类的bill函数)
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<f const Sale&first,const Sale&second)
{
// *****found*****
________;//判断是否first价格低于second价格
}
int main()
{
Sale simple(10.00);
DiScountSale diScount (11.00,90);
if(discount<Simple)
{
cout<<\\
(1)return this->bill()-other.bill()
(2)return fraction*price
(3)return first.bill()<second.bill()
//bill()是返回商品的实际价格
(4)simple.sayings(discount)
解析:Sale类有一个数据成员:保护成员price,表示商品的价格;两个构造函数:默认构造函数Sale()将类成员price初始化为0,构造函数Sale(double the_price)将成员price初始化为the_price;两个成员函数:bill()函数是一个虚函数,返回price的值;savi
本文档预览:3600字符,共5747字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载