首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
使用VC6打开考生文件夹proj2下的工程proj2,其中有两个类:一是销售类(sale),用于表示按照一件商品的基本价格进行销售;另一个是打折销售类(DiscountSale),用于表示在基本价格基础上按一个折扣比例进行销售。DiscountSale类继
使用VC6打开考生文件夹proj2下的工程proj2,其中有两个类:一是销售类(sale),用于表示按照一件商品的基本价格进行销售;另一个是打折销售类(DiscountSale),用于表示在基本价格基础上按一个折扣比例进行销售。DiscountSale类继
admin
2017-12-14
27
问题
使用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
{
//**********found**********
________;//返回当前对象价格比other贵多少的差价
}
class DiscountSale:public Sale//打折销售类继承销售类
{
public:
DiscountSale();//默认构造函数,将discount初始化为0
DiscountSale(double the_price,double the_discount);//构造函数,the_price是基本价格;the_discount是折扣百分比
virtual doublle 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
{
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 first.bill()
解析
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()
(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)
转载请注明原文地址:https://kaotiyun.com/show/WjAp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
若调用一个函数,且此函数中没有return语句,则正确的说法是该函数
在32位的计算机中,一个char型数据所占的内存长度的字节是
在面向对象方法中,属性与操作相似的一组对象称为______。
函数定义为Fun(int&i),变量定义n=100,则下面调用正确的是
结构化程序设计的主要特点是
有如下程序:#include<iostream>usingnamespacestd;intmain(){cout.fill(’*’);cout.width(5);co
在下面的类定义中,错误的语句是classSample{public:Sample(intval);//①~Sample()://②private:in
如下程序声明了一个使用两个通用数据类型的模板类dataClass,其中构造函数用于初始化两个数据成员,成员函数show用于输出两个数据成员的数值。#include<iostream>usingnamespacestd;______
假定要对类AB定义加号操作符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句为()。
在E-R图中,用来表示实体的图形是()。
随机试题
当转子转速达到一阶临界转速时,振动高点滞后于重点的相位角为( )。
技术报告标题应具备准确性、()。
患者,女性,28岁。哺乳期。左乳房胀痛、发热2天。查体:T39.4℃,P106次/分。左乳房外上象限6cm×4cm范围红肿,有明显压痛和波动感。急行切开引流术。错误的手术措施是()
初产妇,25岁,孕足月出现规律性宫缩,1小时后来院,由于宫缩过强,立刻将产妇放在产床上,未来得及消毒及保护会阴,胎儿急速娩出,正处理婴儿时,见阴道有较多血流出。腹部检查,子宫收缩良好。产后出血易发生的时期是
某公司因与个体工商户李某发生债务纠纷,向法院申请诉前保全,查封李某运输汽车一辆。但该公司在法定期间内未对李某起诉,并因保全而给李某造成经济损失。按照我国《民事诉讼法》的规定,李某可以行使下列什么权利?
对于同一宗房地产来讲,只存在着土地、建筑物和房地三种形状,因此,同一宗房地产的价值只能归属于这三种对象。()
下列关于投标人的说法中,错误的是()。
我国第一大发债主体是()。
阅读以下说明,回答问题1至问题4,将解答填入答题纸对应的解答栏内。【说明】某公司网络拓扑结构图如图3-1所示,其中S1为三层交换机。网管员对交换机完成了基本配置,基本配置代码如下所示,请将下面配置代码或注释补充完整。Switch>
A、Itisamutuallybeneficialpractice.B、Itbecomesaninternationalbusiness.C、Itbecomesthebestwayofeducation.D、Itbec
最新回复
(
0
)