首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。DataList的重载运算符函数operator+,其功能是求当前数据表与另一个相同长度的数据表之和;即它返回一个数据表,其每
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。DataList的重载运算符函数operator+,其功能是求当前数据表与另一个相同长度的数据表之和;即它返回一个数据表,其每
admin
2021-06-10
102
问题
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。DataList的重载运算符函数operator+,其功能是求当前数据表与另一个相同长度的数据表之和;即它返回一个数据表,其每个元素等于相应两个数据表对应元素之和。请编写这个operator+函数。程序的正确输出应该是:
两个数据表:
1,2,3,4,5,6
3,4,5,6,7,8
两个数据表之和:
4,6,8,20,12,14
要求:
补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//DataList.h
#include<iostream>
using namespace std;
class DataList{//数据表类
int len;
double*d;
public:
DataList(int len,double data[]=NULL);
DataList(DataList &data);
int length()const{return len;}
double getElement(int i)const{return d
;}
DataList operator+(const DataList& list)const;//两个数据表求和
void show()const;//显示数据表
};
void writeToFile(char*,const DataList&);
//main.cpp
#include"DataList.h"
DataList::DataList(int len,double data[]):len(len){
d=new double[len];
for(int i=0;i<len;i++)
d
=(data==NULL?0.0:data
);
}
DataList::DataList(DataList&data):len(data.len){
d=new double[len];
for(int i=0;i<len;i++)
d
=data.d
;
}
DataList DataList::operator+(const DataList&list)const{//两个数据表求和
double*dd=new double[list.length()];
//**********333**********
//**********666**********
return DataList(list.length(),dd);
}
void DataList::show()const{//显示数据表
for(int i=0;i<len-1;i++)
cout<<d
<<",";
cout<<d[len-1]<<endl;
}
int main(){
double s1[]={1,2,3,4,5,6};
double s2[]={3,4,5,6,7,8};
DataList list1(6,s1),list2(6,s2);//定义两个数据表对象
cout<<"两个数据表:"<<endl:
list1.show();
list2.show();
cout<<endl<<"两个数据表之和."<<end!;
(list1+list2).show();
writeToFile(" ",list1+list2);
return0;
}
选项
答案
for(int i=0;i<len;++i)//遍历对象list中的数组和d数组,把对应的值相加后放到数组dd中。 dd[i]=d[i]+list.d[i];
解析
本题考查DataList类,其中涉及构造函数、动态数组、复制构造函数、const函数和运算符重载。
主要考查考生对重载运算符的掌握,题目要求对两个数据表求和。程序已经定义了动态数组dd,并已经分配好了空间,接下来只要运用循环语句完成元素相加并进行赋值即可。
转载请注明原文地址:https://www.kaotiyun.com/show/J8fp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下程序:classBase{public:intdata;};classDerivedl:publicBase{};classDerived2:protectedBase{};
下列选项中,正确的C++表达式是()。
若AA为一个类,a为该类的非静态数据成员,在该类的一个成员函数定义中访问a时,其书写格式为()。
在结构化程序设计中,模块划分的原则是()。
下列关于派生类构造函数和析构函数的说法中,错误的是()。
若MyClass是一个类名,且有如下语句序列MyClassc1,*c2;MyClass*c3=newMyClass;MyClass&c4=c1;上面的语句序列所定义的类对象的个数是()。
下列有关继承和派生的叙述中,正确的是()。
下列关于栈的叙述正确的是()。
下列叙述中正确的是()。
随机试题
有关食管憩室说法错误的是
原发性肝癌主要应鉴别的疾病是
A.链霉素B.氯霉素C.克林霉素D.红霉素E.四环素最先用于临床的氨基糖苷类药物是
根据《药品流通监督管理办法》,有关医疗机构购进、储存药品的叙述,错误的是
无风险利率对期权的时间价值的影响有()。Ⅰ.当利率提高时,期权的时间价值会减少Ⅱ.当利率提高时,期权的时间价值会增高Ⅲ.当利率下降时,期权的时间价值会增高Ⅳ.当利率下降时,期权的时间价值会减少
下列陈述中,对心理咨询谈话的特征描述正确的有()。
下列有关实践的主体与客体的相互作用的叙述,正确的是( )
TheInternetisanexcellentsourceforfindingmanytypesofinformationandforkeepingupwithnewdevelopmentsintheworld.
Thestatistics______thatlivingstandardsintheareahaveimproveddrasticallyinrecenttimes.
Moresurprising,perhaps,thanthecurrentdifficultiesoftraditionalmarriageisthefactthatmarriageitselfisaliveand【C1
最新回复
(
0
)