首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2。其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为: (
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2。其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为: (
admin
2017-02-21
32
问题
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2。其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为:
(1,2,3,4,5)
(0,0,0,0,0,0)
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream>
using namespace std;
class VectorBase{//向量基类,一个抽象类
int len;
public:
VectorBase (int fen):len(len){}
int length()const{returnlen;)
//向量长度。即向量中元素的个数
virtual double getElement(int
i)const=0,//取第i个元素的值
virtual double sum()const=0;//求所有元素的和
void show()const{//显示向量中所有元素
cout<<"(";
for(int i=0;i<length()-1;i++)
cout<<getElement(i)<<
","
//********found********
cout<<_______<<")"
<<end1;//显示最后一个元素
}
};
class Vector:public VectorBase{//向量类
double *va1;
public:
Vector(int len,double v[]=
NULL):VectorBase(len){
val=new double[len];
for(int i=0;i<len;i++)
val
=(v==NULL?0.0:v
);
}
//**********found**********
~Vector(){_______;}
double getElement(int index)
const{return val[index];)
double sum()const{
double s=0.0;
//**********found**********
for(int i=0,i<length();i++)_______;
return s;
}
};
class ZeroVector:public VectorBase{//零向量类
public:
ZeroVector(int len):VectorBase(len){}
//**********found**********
double getElement(int index)const{_______;}
double sum()const{return 0.0;
}
};
int main(){
VectorBaSe *v;
double d[]={1,2,3,4,5);
V=new Vector(5,d);
V->show();
delete v;
v=new ZeroVector(6);
v->show();
delete v;
return 0;
}
选项
答案
(1)getElement(length()-1) (2)delete[]val (3)s +=val[i] (4)return 0.0:
解析
(1)主要考查考生对成员函数的掌握,题目要求显示最后一个元素。前面有纯虚函数virtual double getElement(int i)const=0,因此可以直接调用getElement函数来取得最后一个元素,注意最后一个元素位置是Ungth()-1而不是length()。
(2)主要考查考生对析构函数的掌握,前面定义了类的私有成员*val,因此析构函数要释放val,使用delete语句完成。
(3)主要考查考生对for循环的掌握,由函数名double sum()const可知,该函数要求元素之和,for循环语句的作用是遍历整个数组,在此使用语句s+=:val
完成程序。
(4)主要考查考生对成员函数的掌握,由该类的注释:零向量类,可以了解到该类的元素都为零,因此无论要取第几个元素都返回0,由于数据类型为double,所以为return0.0。
转载请注明原文地址:https://jikaoti.com/ti/MXt0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
软件开发离不开系统环境资源的支持,其中必要的测试数据属于
下列程序在构造函数和析构函数中申请和释放类的数据成员int*a,申请时使用形参b初始化a,请填空。classA{public:A(intb);~A(
将x+y*z中的“+”用成员函数重载,“*”用友元函数重载应写为【】。
以下程序的输出结果是【】。#include<iostream.h>voidfun(){staticinta=0;a+=2;cout<<
下面叙述错误的是()。
Jackson方法是一种面向【】的结构化方法。
设数组data[m]作为循环队列SQ的存储空间,front为队头指针,rear为队尾指针,则执行出队操作后其头指针front值为( )。
在结构化设计方法中,数据流图表达了问题中的数据流与加工之间的关系,并且,每一个【 】实际上对应一个处理模块。
下列数据结构中,能用二分法进行查找的是
如果有以下定义及初始化:inta=3,*p=&a;则由此可以推断,*p的值是()。
随机试题
Thepolicewillneedtokeepawaryeyeonthisareaoftown.
简述解决组织冲突的主要方式。
女,5个月龄。因发现心脏杂音5天来诊。查体:发育营养良好,无发绀,胸骨左缘3、4肋间可闻Ⅲ/6级收缩期杂音,无震颤。超声心动图检查示:各房室内径正常,室间隔膜部回声中断3mm,可探及左向右过隔血流,诊断:室间隔缺损。根据上述检查,给家长的建议应是
过敏试验最费时间的方法是
适用于高速公路、一级公路沥青下面层及以下层次,二级及二级公路以下公路的各个层次的沥青为()。
【2017年】企业将自有房屋无偿提供给本企业行政管理人员使用,下列各项中,关于计提房屋折旧的会计处理表述正确的是()。
根据下列材料设计教学片段:(1)《普通高中历史课程标准(实验版)》要求:了解中国近代以来人们物质生活和社会习俗变化的史实,探讨影响其变化的因素。(2)课文摘录:鸦片战争以后,西方列强在中国通商口岸设厂、开店、办银行,还舶来一些洋货,西方文化逐步渗透
下列戏剧中,节选出折子戏“苏三起解”的是()。
上课听讲、师傅带徒弟、母鸡带小鸡都是教育现象。()
2014年2月5日,马某与陈某订立一份房屋买卖合同,约定陈某购买马某的房屋一套(以下称1号房),价格80万元。并约定,合同签订后一周内陈某先付20万元,交付房屋后付30万元,办理过户登记后付30万元。2月8日,梁某得知马某欲将该房屋出卖,表示愿出90万元购
最新回复
(
0
)