使用 VC6 打开考生文件夹 proj2 下的工程 proj2, 其中有两个类: 一是销售类(sale), 用于表示按照一件商品的基本价格进行销售; 另一个是打折销售类(DiscountSale), 用于表示在基本价格基础上按一个折扣比例进行销售。 DiscountSale 类继承了 sale 类。 类的主要数据成员的含义和成员函数的功能要求在程序注释中有说明。 请在程序中的横线处填写适当的代码, 然后删除横线, 完成程序的功能。 此程序的正确输出结果应为:
Discount item is cheaper.
Saving is 0.1
注意: 只能在横线处填写适当的代码, 不要改动程序中的其他内容。
#include<iostream>
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<(const Sale&first,const Sale&second)
{
//******found******
______;//判断是否 first 价格低于 second 价格
}
int main()
{
Sale simple(10.00);
DiscountSale discount(11.00,90);
if(discount<simple)
{
cout<<"Discount item is cheaper.\n";
//******found******
//这里输出购买 discount 比购买 simple 节省多少钱
cout<<"Saving is"<<______<<endl;
}
else
cout<<"Discount item is not cheaper.\n";
return 0;
}_______
(1)return this->bill()-other.bill();
(2)return fraction*price;
(3)return first.bill()<second.bill() //bill()是返回商品的实际价格;
(4)simple.savings(discount)。
主要考查类的数据成员、 类的成员、 虚函数与多态性以及重载关系运算符。
Sale 类有一个数据成员: 保护成员 price, 表示商品的价格; 两个构造函数:默认构造函数 Sale()将类成员 price 初始化为 0, 构造函数 Sale(double the_price)将成员 price初始化为 the_price; 两个成员函数: bill()函数是一个虚函数, 返回 price 的值; savings()函数返回参数 other 所引用的对象比当前对象便宜的差价。
DiscountSale 类继承 Sale 类, 它有两个数据成员: 保护成员 price 继承于 Sale 类, 表示商品价格, 保护成员 discount 表示商品折扣百分比; 两个成员函数: savings()函数继承于 Sale 类;bill()函数继承 Sale 类并被改写。
main()函数中, 首先构造两个类对象: Sale 类对象 simple 和 DiscountSale 类对象 discount,其中 simple 对象的 price 被赋值为 10.00; discount 对象的 price 被赋值为 11.00, 成员 discount被赋值为 90, 即折扣为 90%。
(1)if语句的条件中, 将discount与 simple进行比较, 如果main()主数的 discount小于 simple,那么说明 discount 的实际价格比 simple 价格便宜; 所以需要重载“<” 运算符, 题意中重载的“<” 运算符函数接收两个 Sale 类对象的引用, 通过对象的引用, 比较两个对象的实际价格, 所以重载“<” 运算符函数体中, 若 first 对象引用的商品实际价格小于 second 对象引用的商品实际价格, 那么函数值返回 true, 可知函数体中需要补充的语句如下:
return first.bill()<second.bill();//bill()是返回商品的实际价格
(2)由于 bill()是虚函数, 且重载“<” 运算符函数的参数是引用, 所以调用 bill()函数时, 使用了多态机制, 引 用的对象是 Sale 类对象, 调用 Sale 类中的 bill()函数, 引 用的对象是DiscountSale 类对象, 那么将调用 DiscountSale 类的 bill()函数, bill()函数体中, 需要返回的是商品的实际价格, 即折扣完的价格, 所以需要将返回打过折的价格: DiscountSale 类中,bill()函数体中需要补充的语句如下:
return fraction*price
(3)savings()函数需要计算当前对象比 other 引用的对象在价格上贵多少, 所以 savings()函数体中需要补充的语句如下:
return this->bill()-other.bill()
同样, this 指针引用当前对象, other 是引用对象, 所以可以与 bill()虚函数一起使用多态机制, 获得当前对象和 other 对象的实际价格, 再进行差值计算。
(4)main()函数中, 程序判断 discount 小于 simple 后, 输出提示信息, 再调用 savings()函数输出 discount 和 simple 的差值, 这里需要计算 discount 比 simple 节省多少钱, 所以需要将simple 当做当前对象, discount 当做参数传入 savings()函数, 所以补充语句如下:
simple.savings(discount)