首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2。其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为: (1.2,
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2。其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为: (1.2,
admin
2016-08-19
42
问题
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2。其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为:
(1.2,3,4.5)
(0,0,0,0,0,0)
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
1 {}include
2 using namespace std;
3 class VectorBase{//向量基类,一个抽象类
4 int len;
5 public:
6 VectorBase(int len):len(1en){]
7 int length()const{return len;}//向量长度,即向量中元素的个数
8 virtual double getElement(int i)const=0;//取第i个元素的值
9 virtual double sum()const=0;//求所有元素的和
10 void show()const{//显示向量中所有元素
11 cout<<"(";
12 for(int i=0;i
13 cout<
14 //**********found**********
15 cout<<_______<<")"<
16 }
17 };
18 class Vector:public VectorBase{//向量类
19 double*val;
20 public:
21 Vector(int fen,double v[]=NULL):VectorBase(fen){
22 val=new double[len];
23 for(int i=0;i
=(v=NULL?0.0:v
);
24 }
25 //**********found**********
26 ~Vector(){_____;}
27 double getElement(int index)const{return val[index];}
28 double sum()const{
29 double s=0.0;
30 //**********found**********
31 for(int i=0;i
32 return s;
33 }
34 };
35 class ZeroVector:public VectorBase {//零向量类
36 public:
37 ZeroVector(int len): VectorBase(len){}
38 //**********found**********
39 double getElement(int index)const{_______;}
40 double sum()const{return 0.0 ;}
41 },
42 int main(){
43 VectorBase*v;
44 double d[]={1,2,3,4,5};
45 v=new Vector(5,d);
46 v->show();
47 delete v;
48 v=new ZeroVector(6);
49 v->show();
50 delete v;
51 return 0;
52 }
选项
答案
(1)getElement(1ength()-1) (2)delete[]val (3)s+=val[i] (4)return 0.0:
解析
(1)主要考查考生对成员函数的掌握,题目要求显示最后一个元素。前面有纯虚函数virtual double getElement(int i)const=0,因此可以直接调用getElement函数来取得最后一个元素,注意最后一个元素位置是Length()-1而不是Length()。
(2)主要考查考生对析构函数的掌握,前面定义了类的私有成员*val,因此析构函数要释放val,使用delete语句完成。
(3)主要考查考生对for循环的掌握,由函数名doublesum()const可知,该函数要求元素之和,for循环语句的作用是遍历整个数组,在此使用语句S+=val
完成程序。
(4)主要考查考生对成员函数的掌握,由该类的注释:零向量类,可以了解到该类的元素都为零,因此无论要取第几个元素都返回0,由于数据类型为double,所以为return0.0。
转载请注明原文地址:https://jikaoti.com/ti/QRE0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
以于下面定义的类MyClass,在函数f()中将对象成员n的值修改为50的语句应该是()。classMyClass{public:MyClass(intx){n=x;
数据库设计有两种方法,它们是()。
下列关于goto语句的描述中,正确的是()。
已知有定义constintD=5;inti=1;doublef=0.32;charc=15;则下列选项错误的是
下面是用来计算n的阶乘的递归函数,请将该函数的定义补充完整。(注:阶乘的定义是n!cn*(n-1)*...*2*1)unsignedfact(unsignedn){if(n<=1)return1;retur
下列关于纯虚函数的描述中,正确的是()。
函数YangHui的功能是把杨辉三角形的数据赋给二位数组的下半三角,形式如下:其构成规律是:①第0列元素和主对角线元素均为1。②其余元素为其左上方和正上方元素值之和。③数据的个数每行递增1。请将程序补充完整。
类模板不同于函数模板,类模板只可用【】型参数。
fstream、ifstream和ofstream文件流类的成员函数______关闭文件。
下列关于队列的叙述中正确的是
随机试题
严重腹型紫癜患儿应当给予( )。
下列传染病不属于乙类的是
为了研究某中药牙膏对人群牙周健康的影响,随机选取40人用该牙膏刷牙,4个月后进行临床评价,该研究方法是
对于验收不合格的材料、构配件,()应签发监理工程师通知单,书面通知承包单位限期将不合格的工程材料、构配件撤出现场。
建设单位应当自建设工程竣工验收合格之日起15日内,将建设工程竣工验收报告和()部门出具的认可文件或准许使用文件报建设行政主管部门备案。
交通信号灯有红、黄、绿三种,其中停车符号用红色,其原理是()。
红学索隐派,是指研究《红楼梦》的一个派别,义称政治索隐派。所谓索隐即透过字面探索作者隐匿在书中的真人真事。索隐派的主要手段是大作繁琐的考证,从小说的情节和人物中考索出“所隐之事,所隐之人”。根据以上定义,下列各项属于索隐派的是()。
若某人持有盗版软件,但他本人确实不知道该软件是盗版的,则(10)承担侵权责任。
下列实际吞吐量为5~7Mbit/s,最大容量为33Mbit/s的IEEE802.11标准是()。
Wecommonlythinkofsportsmanshipinconnectionwithathleticcontests,butitalsoappliestoindividualoutdoorsports.Note
最新回复
(
0
)