问答题

请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中定义了Component类、Composite类和Leaf类。Component是抽象基类,Composite和Leaf是Component的公有派生类。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
Leaf Node
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include<iostream>
using namespace std;
class Component
public:
//声明纯虚函数print()
//**********found**********
;
class Composite:public Component
public:
//**********found**********
void setChild(______)

m_child=child;

virtual void print()const

m_child->print();

private:
Component*m_child;
;
class Leaf:public Component
public:
virtual void print()const

//**********found**********
______

;
int main()

Leaf node;
Composite comp;
comp.setChild(&node);
Component*p=∁
p->print();
return 0;

【参考答案】

(1)virtual void print()const=0;
(2)Component*child
......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中定义的Matrix是一个用于表示矩阵的类。成员函数max_value的功能是求出所有矩阵元素中的最大值。例如,若有3×3矩阵 则调用max_value函数,返回值为3。请编写成员函数max_value。 要求: 补充编制的内容写在“ ********333********”与“ ********666********”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 Matrix.h #include <iostream> #include <iomanip> using namespace std; const int M=18; const int N=18; class Matrix int array[M][N]; public: Matrix() int getElement(int i,int j)constreturn array[i][j]; void setElement(int i,int j,intvalue)array[i][j]=value; int max value()const; void show(const char*s)const cout<<endl<<s; for(int i=0;i<M;i++) cout<<endl; for(int j=0;j<N;j++) cout<<setw(4)<<array[i][j]; ; void readFromFile(const char*,Matrix&); void writeToFile(char*,constMatrix&); main.cpp #include Matrix.h #include <fstream> void readFromFile(const char* f,Matrix& m) ifstream infile(f); if(infile.fail ()) cerr<< 打开输入文件失败! ; return; int k; for(int i=0;i<M;i++) for(int j=0;j<N;j++) infile>>k; m.setElement(i,j,k); int Matrix::max value()const ********333******** ********666******** int main() Matrix m; readFromFile( ,m); m.show( Matrix: ); cout<<endl<< 最大元素: <<m.maxvalue()<<endl; writeToFile( ,m); return 0;