首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #include #include using namespace std; class Animal{ public: virtual string
有如下程序: #include #include using namespace std; class Animal{ public: virtual string
admin
2021-05-06
28
问题
有如下程序:
#include
#include
using namespace std;
class Animal{
public:
virtual string GetType() const { return "Animal"; }
virtual string GetVoice() const { return "Voice"; }
};
class Dog:public Animal{
public:
string GetType() const { return "Dog"; }
string GetVoice() const { return "Woof"; }
};
class Cat:public Animal{
public:
string GetType() const { return "Cat"; }
string GetVoice() const { return "Miaow"; }
};
void Type(Animal a) { cout<
void Speak(Animal a) { cout<
int main() {
Dog d; Type(d); cout<<" speak "; Speak(d); cout<<" - ";
Cat c; Type(c); cout<<" speak "; Speak(c); cout<
return 0;
}
运行时的输出结果是
选项
A、Dog speak Voice - Cat speak Voice
B、Dog speak Woof - Cat speak Miaow
C、Animal speak Voice - Animal speak Voice
D、Animal speak Woof - Animal speak Miaow
答案
C
解析
本题考查虚函数的运用,本题中定义Dog d; Type(d)时,执行基类的Type函数,输出Animal,然后输出speak,然后执行基类的Speak函数输出Voice,最后输出-,同理cat输出类似,所以结果为C选项正确。
转载请注明原文地址:https://kaotiyun.com/show/xGfp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下程序:#include<iostream>#include<iomanip>usingnamespacestd;intmain(){cout<<setfill(’#’)<<setw(4)<<"OK"<<123<<endl;retur
定义派生类时,若不使用关键字显式地规定采用何种继承方式,则默认方式为()。
若AA为一个类,a为该类的非静态数据成员,在该类的一个成员函数定义中访问a时,其书写格式为()。
有如下类定义:ciassA{char*a;public:A():a(0){}A(char*aa){//把aa所指字符串拷贝到a所指向的存储空间a=__________;strcpy(
在一个派生类的成员函数中,试图调用其基类的成员函数“voidf();”,但无法通过编译。这说明()。
有如下程序:#include<iostream>usingnamespacestd;intmain(){hatsum;for(inti=0;i<6;i+=3){sum=i;fo
下列情况中,不会调用拷贝构造函数的是()。
使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为:numbor1=anumher2=anumbcr1=anumber2_b注意:错误的语句
某二叉树共有12个结点,其中叶子结点只有1个。则该二叉树的深度为(根结点在第1层)()。
下列关于运算符重载的叙述中,错误的是()。
随机试题
火场中防止烟气危害最简单的方法是()。
茶文化结构中的精神文化是茶文化的核心部分。
当社会需求不足时,政府应采取的措施有
《天净沙.秋思》中,概括全篇主题思想的是()
A.完全再生B.瘢痕修复C.两者均有D.两者均无二期愈合
下列有关破伤风病人临床表现的描述中,错误的是
《中华人民共和国民法典》的制定起草工作属于以下哪种规范性法律文件系统化的方式?
在法律上,学校与学生的关系是属于()
对任意的x,y有将f(x,y)变换成g(u,v),试求满足﹦u2﹢v2的常数a,b。
COSTANDBENEFITSOFSOCIALLIFE(1)Manythinkthatthereasonwhysomanyanimalslivewithothersoftheirspeciesistha
最新回复
(
0
)