首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中有矩阵基类MatrixBase、矩阵类Matrix和单位阵UnitMatrix的定义,还有main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中有矩阵基类MatrixBase、矩阵类Matrix和单位阵UnitMatrix的定义,还有main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输
admin
2020-06-29
33
问题
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程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 1 0
0 0 0 0 0 1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream>
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<rows;i++){
cout<<end1:
for(int j=0;j<cols;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)const{ 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;
return 0.0;
}
};
int main(){
MatrixBase * m;
double d[][5]={{1,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<<end1;
m=new UnitMatrix(6);
m->show();
delete m;
return 0;
}
选项
答案
(1)getElement(i,j) (2)MatrixBase(rows,cols) (3)now double[rows*cols] (4)r==e
解析
(1)主要考查考生对纯虚函数的掌握,函数功能是分行显示矩阵中所有元素。因此在这里要输出行为i、列为j的元素,使用纯虚函数getElement(i,j)实现,输出语句为cout<<getElement(i,j)<<" ";。
(2)主要考查考生对派生类的构造函数的掌握,派生类的构造函数使用成员列表初始化法,先对基类初始化。
(3)主要考查考生对动态数组的掌握,val是double型指针,要给val赋值,就要先给它分配空间,应使用new来完成。
(4)主要考查考生对成员函数的掌握,因为要输出单位矩阵,只有满足条件r=c的元素为1.0,所以填写语句if(r==c)return 1.0;。
转载请注明原文地址:https://jikaoti.com/ti/lUA0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
C++语言程序的注释可以出现在程序中的任何地方,一个注释以【】作为开始和结束的标记。
下列程序的输出结果为2,请将程序补充完整。#inelude<iostream.h>usingnamespacestd;classBase{public:______voidfun(){cout<<
下面程序的运行结果为#include<iostream.h>classA{intnum;public:A(inti){num=i;}A(A&a){num=a.n
在说明一个结构体变量时,系统分配给它的存储空间是()。
下列函数的功能是判断字符串str是否对称,对称则返回true,否则返回false。请在画线处填上适当内容,实现该函数。boolfun(char*str)}inti:0,j=0;while(str[j])(6);
有如下程序段:inti=5;while(inti=0){cout<<‘*’;i~;)运行时输出“*”的个数是()。
下列有关数据库的描述,正确的是()。
假定MyClass为一个类,则该类的拷贝初始化构造函数的声明语句为()。
某个工厂有若干个仓库,每个仓库存放有不同的零件,相同零件可能放在不同的仓库中。则实体仓库和零件间的联系是
若实体A和B是一对一的联系,实体B和C是多对一的联系,则实体A和C的联系是
随机试题
简要说明工作时间法律调整的意义。
毛泽东探索中国社会主义建设道路所取得的理论成果集中体现在下列哪些著作中
甲公司于2013年10月10日通过拍卖方式拍得位于北京郊区的一块工业建设用地;同年10月15日,甲公司与北京市土地管理部门签订《建设用地使用权出让合同》:同年10月21日,甲公司缴纳全部土地出让金;同年11月5日,甲公司办理完毕建设用地使用权登记,并获得建
(2006年)2006年3月20日,上海的甲公司与北京的乙公司签订了一份买卖合同,约定:甲公司向乙公司购买1000吨化工原料,总价款为200万元;乙公司在合同签订后1个月内交货,甲公司在验货后7日内付款。双方没有明确约定履行地点。合同签订后,甲公司以其办公
下列属于问题解决的是()。
关于行政处罚说法正确的是()。
Thebasiccausesareunknownthoughcertainconditionsthatmayleadtocancerhavebeen______.
下列属于我国法律规定的免责条件或情况的有()。(2009年多选47)
下列不属于CCMD一3中神经症的诊断是()。
PassengersarerequestedtonotethatthenewtimetablecomesintoeffectfromMay1,1997.
最新回复
(
0
)