首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
使用VC6打开考生文件夹proj2下的工程proj2,其中有两个类:一是销售类(sale),用于表示按照一件商品的基本价格进行销售;另一个是打折销售类(DiscountSale).用于表示在基本价格基础上按一个折扣比例进行销售。DiscountSale类继
使用VC6打开考生文件夹proj2下的工程proj2,其中有两个类:一是销售类(sale),用于表示按照一件商品的基本价格进行销售;另一个是打折销售类(DiscountSale).用于表示在基本价格基础上按一个折扣比例进行销售。DiscountSale类继
admin
2017-07-28
39
问题
使用VC6打开考生文件夹proj2下的工程proj2,其中有两个类:一是销售类(sale),用于表示按照一件商品的基本价格进行销售;另一个是打折销售类(DiscountSale).用于表示在基本价格基础上按一个折扣比例进行销售。DiscountSale类继承了sale类。类的土要数据成员的含义和成员函数的功能要求在程序注释中有说明。请在程序中的横线处填写适当的代码,然后删除横线,完成程序的功能。此程序的正确输出结果应为:
Discount item is cheaper.
Saving is0.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
{
//**********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
};
DiscountSaie::DiscountSale():discount(0){}
DiscountSale::DiscountSale(double the_price,double the_discount)
:Sale(the_price),discount(the_discount){}
double DiscountSale::bill()const
{
doubte 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":
return0;
}
选项
答案
(1)return this->bill()-other.bill() (2)return fraction*price (3)return first.bill()<second.bill()//bill()是返回商品的实际价格 (4)simpte.savings(discount)
解析
主要考查类的数据成员、类的成员、虚函数与多态性以及重载关系运算符。
Sale类有一个数据成员:保护成员price,表示商品的价格;两个构造函数:默认构造函数Sale()将类成员price初始化为0,构造函数Sale(double the_priee)将成员price初始化为the_price;两个成员函数:bill()函数是一个虚函数,返回price的值;saVings()函数返回参数other所引用的对象比当前对象便宜的差价
DiscountSale类继承Sale类,它有两个数据成员:保护成员price继承于Sale类,表示商品价格,保护成员discount表示商品折扣百分比;两个成员函数:savings()函数继承于Sale类;bill()函数继承Sale类并被改写。
main()函数中,首先构造两个类对象:Sale类对象simple和DiseountSale类对象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)
转载请注明原文地址:https://jikaoti.com/ti/Dmt0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
设有以下变量定义,并已赋确定的值,charw;intx;floaty;doublez;则表达式:w*x+z-y所求得的数据类型为______。
函数定义的格式是【 】函数名(形式参数表)函数体。
在派生类中,重载一个虚函数时,要求函数名、参数的个数、参数的类型、参数的顺序和函数的返回值( )。
如果一个类含有一个以上的纯虚函数,则称该类为
数据库系统阶段的数据具有较高独立性,数据独立性包括物理独立性和______两个含义。
程序的3种基本控制结构是()。
在用class定义一个类时,数据成员和成员函数默认的访问权限是【】:
联接运算是将两个或两个以上的关系根据联接条件生成一个【】。
C++类体系中,不能被派生类继承的有()。
为了在两个关系中提取所有相同的元组,则在交与并这两种关系运算中,应使用【 】运算。
随机试题
有关Lidle综合征的叙述,错误的是
二尖瓣狭窄时,首先引起心脏的
常某涉嫌投毒杀人被立案侦查,考虑到常某怀孕已近分娩,县公安机关决定对其取保候审,责令其交纳保证金3000元。婴儿出生1个月后,常某写下遗书,两次自杀未果,家人遂轮流看护常某及其婴儿,以防意外。在此情况下,对常某应当采取什么强制措施?(2005—卷二—28,
甲、乙、丙三个自然人拟共同投资设立一家生产文具用品的有限责任公司,三人草拟的公司章程规定:(1)出资方式:甲,现金5万元;乙,劳务折价2万元;丙,机器设备3万元。(2)出资期限:公司股东分两次出资,首次出资为现金5万元,由甲在公司领取营业执照后3日内缴
根据下面的文字资料回答下面问题北大校办企业产值从1990年的10亿元人民币增加到2000年底的120亿元人民币,每年增幅超过30%,上缴国家利税20亿元,给学校回报近4亿元。
设试讨论f(x)在x=0处的连续性和可导性.
将考生文件夹下PAINT文件夹中的文件USER.TXT移动到考生文件夹下JINK文件夹中,并改名为TALK.TXT。
A.BankofAmericaannouncedthatitwasreadytorepaythe$45billionitreceivedingovernmentbail-outmoney,whichwillfin
WhenGeorgeW.BushdeliveredhisStateoftheUnionAddressthispastJanuary,threewordsappearedthathadnotpreviouslybee
AhardBrexitposesriskstotheintegrityoffinancialmarketsandcouldmakeithardertoprotectconsumersfromwrongdoingby
最新回复
(
0
)