首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj2,其中定义了Component类、Composite类和Leaf类。Component是抽象基类,Composite和Leaf是Component的公有派生类。请在横线处填写适当的代码并删除横线,以实现上述类定义。
请打开考生文件夹下的解决方案文件proj2,其中定义了Component类、Composite类和Leaf类。Component是抽象基类,Composite和Leaf是Component的公有派生类。请在横线处填写适当的代码并删除横线,以实现上述类定义。
admin
2019-04-24
18
问题
请打开考生文件夹下的解决方案文件proj2,其中定义了Component类、Composite类和Leaf类。Component是抽象基类,Composite和Leaf是Component的公有派生类。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
Leaf Node
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include
using namespace std;
class Component{
public:
//声明纯虚函数print()
//********found********
};
class Composite:public Component{
public:
//********found********
void setChiid(___________)
{
m child=child;
}
virtual void print()const
{
m chiid->print();
}
private:
Component*m_chiid;
};
class Leaf:public Component{
public:
virtual void print()const
{
//********found********
}
};
int main()
{
Leaf node;
Composite comp;
comp.setChiid(&node);
Component*p=∁
p->print();
return 0;
}
选项
答案
(1)virtual void print()const=0; (2)Component}chiht (3)cout<<"Leaf Node"<
解析
(1)主要考查考生对纯虚函数的掌握,题目要求声明纯虚函数print()。在其派生类中print()函数的定义为virtual void print()const,由此可知纯虚函数为virtual void print()const=0。
(2)主要考查考生对成员函数的掌握,题目要求填写函数void setChild的形参,由setChild的函数体可知形参为child,再看类的私有成员m_child的定义:Component*m_child;。由此可知形参为:Component*child。
(3)主要考查考生对纯虚函数的掌握,先看主函数的程序:
Leaf node;
Composite comp ;
comp.setChild(&node);
Component*p=&comp;
p->print();
第一条和第二条语句都是定义语句,第三条语句调用函数setChild,由setChild函数的定义可知,comp中的m_child等于node,第四条语句定义了个指针P指向comp的地址,也就是node,最后一条语句通过指针p调用函数print,也就是调用类Leaf的函数print,因为题目要求输出:Leaf Node,因此在这里添加语句:cout<<"Leaf Node"<
转载请注明原文地址:https://jikaoti.com/ti/gYt0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下程序:#include<iostream>usingnamespacestd;classMonitor{public:Monitor(chart):type(t){}voidPr
有如下程序段:charC[20]="examination";C[4]=0;cout<<c<<endl;这个程序的输出结果是______。
如下程序的输出结果是______。#include<iostream>usingnamespacestd;classWages{//“工资”类doublebase;//基本工资dou
在数据库管理系统提供的数据定义语言、数据操纵语言和数据控制语言中,______负责数据的模式定义与数据的物理存取构建。
下面对对象概念描述正确的是()
一个在基类中说明的虚函数,它在该基类中没有定义,但要求任何派生类都必须定义自己的版本,此虚函数又称______。
有如下类声明:classTestClass1{intk;public:voidset(intn){k=n;}intget()const{returnk;}};classT
下面对静态数据成员的描述中,正确的是()
对于派生类的构造函数,在定义对象时构造函数的执行顺序为:先执行调用______的构造函数。再执行调用子对象类的构造函数,最后执行派生类的构造函数体中的内容。
下列字符串中,正确的C++标识符是()。
随机试题
目前语法学界最通行的析句方法有两种,一种是_____,另一种是_____。
对在喷粉室落地的粉末涂料和回收设备回收的粉末涂料,处理干净后,经过一定目数的筛子,再与新粉按比例混合后可使用。
男,39岁,诊断为库欣病。经蝶鞍手术未发现微腺瘤。行双肾上腺切除术,术后出现皮肤发黑。目前诊断考虑
有关麻醉药成人一次限量,下列哪些是恰当的
患者,男,65岁。食管癌手术,留置胃管。术后4天病人咳嗽,痰略带黄色,体温38.5℃。气急,右下肺闻及较多细湿啰音。X线胸片示右肺下大片炎性病变。其最可能的病原体是
洋地黄中毒时心电图变化中,最常见的为
急性白血病病人出血的原因主要是
十一届全国人大三次会议通过了关于修改选举法的规定,我国城乡居民首次实现()。
20世纪70年代,袁隆平第一个开发出可以广泛种植的杂交水稻。()
某美发厅经理在连续几年超额完成了计划指标后,为职工在劳保福利方面做了这样四件事:①投保20万元企业财产保险;②为每个职工投2000元家庭财产保险;③给70多名职工投了十年人身意外事故安全保险;④投保职工失业救济保险,假使企业倒闭,保险公司还可发给职工六个月
最新回复
(
0
)