首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj2,其中定义了Component类、Composite类和Leaf类。Component是抽象基类,Composite和Leaf是Component的公有派生类。请在横线处填写适当的代码并删除横线,以实现上述类定义。
请打开考生文件夹下的解决方案文件proj2,其中定义了Component类、Composite类和Leaf类。Component是抽象基类,Composite和Leaf是Component的公有派生类。请在横线处填写适当的代码并删除横线,以实现上述类定义。
admin
2019-04-24
19
问题
请打开考生文件夹下的解决方案文件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://kaotiyun.com/show/mOAp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下程序段:for(inti=i;i<=50;i++){if(i%31=0)continue;elseif(i%51=0)continue;cout<<i<<",";这个程序的输出结果是_____
软件生命周期可分为多个阶段,一般分为定义阶段、开发阶段和维护阶段。编码和测试属于______阶段。
下面关于虚函数的描述中正确的是()
有如下函数模板定义:templateT1sum(T1a1){return(a1+a2+a3);}则以下调用中正确的是()。
不能作为函数重载的判断依据的是()。
假定MyClass为一个类,则该类的拷贝初始化构造函数的声明语句为()
在下面的类定义中,错误的语句是()。classSample{public:Sample(intval)://①~Sample();//②private:int
派生类的成员函数不能访问基类的
指针变量所保存的不是一般的数据值,而是程序中另一个对象的______。
下面的符号中不属于C++关键字的是()。
随机试题
以下电气设备中,属于防火重点设备的有()。
各种悬吊灯具离地面的距离不应小于()。
英国国家标准(),日本国家标准(),美国国家标准()。
通过把多项式分解因式,把函数f(x)=ln(1+X一2x2)展开为x的幂级数,并给出收敛域.
治疗小儿衣原体肺炎首选的抗生素是()。
房地产权属档案是权属管理活动中形成的历史记录,不但具有专业性、动态性、法律性,还具有()。
管线共沟敷设时,不符合规定的是()。
下列说法中对税务机关职权表述正确的是( )。
商业银行有效的战略风险管理应当确保其长期战略、短期目标、()和可利用资源紧密联系在一起。
天地悠悠悬秤杆,百姓芸芸秤砣心。严纲纪、正作风是一场大手术,唯有___________才能___________。填入画横线部分最恰当的一项是()。
最新回复
(
0
)