首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj2,其中有矩阵基类MatrixBase、矩阵类Matrix和单位阵UnitMatrix的定义,还有main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 1 2
请打开考生文件夹下的解决方案文件proj2,其中有矩阵基类MatrixBase、矩阵类Matrix和单位阵UnitMatrix的定义,还有main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 1 2
admin
2020-12-21
28
问题
请打开考生文件夹下的解决方案文件proj2,其中有矩阵基类MatrixBase、矩阵类Matrix和单位阵UnitMatrix的定义,还有main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 l 0
0 0 0 0 0 1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****。
#include
using namespace std;
//矩阵基础类,一个抽象类
class MatrixBase{
int rows,cols;
public:
MatrixBase(int roWS,int cols):roWS(rOWS),cols(cols){}
int getRows()const{return rows;}//矩阵行数
int getCols()const{return cols;}//矩阵列数
virtual double getElement(int r,int c)const=0;//取第i个元素的值
void show()const{//分行显示矩阵中所有元素
for(int i=0;i
cout<<endl:
for(int j=0;j
//**********found**********
cout<<________<<““:
}
}
};
//矩阵类
class Matrix:public MatrixBase{
double*val:
public:
//**********found**********
Matrix(int rOWS,int cols,double m[]=NULL): ________}
//**********found**********
val=________;
for(int i=0;i<rows* cols;i++)
val
=(m==NULL?0.0:m
);
}
~Matrix(){delete[]val;}
double getElement(int r,int c)eonst{return val[r*getCols()+c];}
};
//单位阵(主对角线元素都是1,其余元素都是0的方阵)类
class UnitMatrix:public MatrixBase{
public:
UnitMatrix(int rows):MatrixBase(rows,rows){}
//单位阵行数列数相同
double getElement(int r,int c)const{
//**********found**********
if(________)return 1.0:
retulm 0.0;
}
};
int main(){
MatrixBase*m;
double d[][5]:{{l,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}};
m=new Matrix(3,5,(double*)d);
m->show();
delete m;
cout<<endl:
m=new UnitMatrix(6);
m->show();
delete m:
return 0;
}
选项
答案
(1)getElement(i,j) (2)MattixBase(rows,cols) (3)new double[rows*cols] (4)r==c
解析
本题考查MatrixBase类及其派生类Matrix和unitMatrix,其中涉及构造函数、const函数、纯虚函数、动态数组和析构函数。派生类的构造函数要涉及基类的初始化,因此必须使用成员初始化列表。动态数组要先使用new语句分配空间,再赋值(1)主要考查考生对纯虚函数的掌握,函数功能是分行显示矩阵中所有元素。因此在这里要输出行为i、列为j的元素,使用纯虚函数getElement(i,j)实现,输出语句为cout<
转载请注明原文地址:https://jikaoti.com/ti/rMl0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下列字符串中可以用作C++标识符的是
下列有关抽象类和纯虚函数的叙述中,错误的是
有如下程序: #include <iostream> using namespace std; class Part{ public: Part(int x=0):val(x) { cout<<val; }
针对数组定义int data[10];,下列表述中错误的是
有如下程序: #include<iostream> using namespace std; class BASE { public: ~BASE(){cout<<"BASE";} }; cl
在学生管理的关系数据库中,存取一个学生信息的数据单位是
设有课程关系模式:R(C#,Cn,T,TA)(其中,C#为课程号,Cn为课程名,T为教师名,Ta为教师地址)并且假定不同课程号可以有相同的课程名,每个课程号下只有一位任课教师,但每位教师可以有多门课程。关系R中对主属性的传递依赖为
在关系数据库中,描述全局数据逻辑结构的是
一个教师讲授多门课程,一门课程由多个教师讲授。则实体教师和课程间的联系是
下列关于赋值运算符“=”重载的叙述中,正确的是()。
随机试题
在组织的运行过程中,遇到冲突或问题时,管理者必须善于处理冲突和解决问题,这时管理者扮演的角色是()
反映肾小管功能的试验为
张某拟为其妻王某投保以死亡为给付条件的人寿险,以下说法不正确的是:()
电焊机二次线可采用()。
在环境影响报告书中的附录和附件中,将建设项目()等必要的有关文件、资料附在环境影响报告书后。
第二类危险源包括()。
下列各项税金中,可能列入“主营业务税金及附加”科目核算的有()。
《中华人民共和国义务教育法》属于()
在夏夜星空的某一区域,有七颗明亮的星星:A星、B星、c星、D星、E星、F星、G星。它们由北至南排列成一条直线,同时发现:(1)c星与E星相邻。(2)B星和F星相邻。(3)F星与C星相邻。(4)G星与位于最南侧的那颗星
Borrowingtostartabusinessisnoteasy.Gettingabankloan,particularlyforanewsmallbusiness,islikegoingthroughthe
最新回复
(
0
)