使用VC6打开考生文件夹proj2下的工程proj2,其中有两个类:一是销售类(sale),用于表示按照。件商品的基本价格进行销售;另一个是打折销售类(DiscountSale),用于表示在基本价格基础上按一个折扣比例进行销售。DiscountSale类继

admin2021-06-10  24

问题 使用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()eonst
{
    return price;
}
double Sale::savings(const Sale&other)const
{
    //**********found**********
    ____________;//返回当前对象价格比other贵多少的差价
}
class DiscountSaLle: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 DiseountSale::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    {
    cout<<"Discount item is cheaper.\n";
    //**********found**********
    //这里输出购买discount比购买simple节省多少钱
    cout<<”Saving is”<<__________<    }
    else
    cout<<"Discount item is not cheaper.\n";
    return 0;
}

选项

答案(1)return this->bill()-other.bill() (2)return:fraction*price (3)return firsl,.bill()
解析 Sale类有一个数据成员:保护成员price,表示商品的价格;两个构造函数:默认构造函数Sale()将类成员price初始化为0,构造函数sale(double the_price)将成员price初始化为the—price;两个成员函数:bill()函数是一个虚函数,返回price的值;savings()函数返回参数t)ther所引用的对象比当前对象便官的差价。
    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()    (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(diseount)
转载请注明原文地址:https://kaotiyun.com/show/Wvfp777K
0

相关试题推荐
随机试题
最新回复(0)