首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
下列程序的输出结果为2,横线处应添加语句( )。 #include using namespace std; class TestClass 1 { public: _________
下列程序的输出结果为2,横线处应添加语句( )。 #include using namespace std; class TestClass 1 { public: _________
admin
2012-12-29
54
问题
下列程序的输出结果为2,横线处应添加语句( )。
#include
using namespace std;
class TestClass 1
{
public:
____________void fun(){cout<<1;}
};
class TestClass2:public TestClass 1
{
public:void fun(){cout<<2;}
};
intmain()
{
TestClass 1*p=new TestClass2;
P->fun();
deletep;
return0;
}
选项
A、public
B、private
C、virtual
D、protected
答案
C
解析
由主函数main入手,定义TestClass1类的指针对象p指向派生类TestClass2。因为基类和派生类中都有fun函数,题目要求输出为2,就是基类对象访问派生类中fun函数。通过虚函数与指向基类对象的指针变量的配合使用,就能方便调用同名函数。所以这里将基类中的fun函数声明为virtual。并且当一个成员函数被声明为虚函数后,其派生类中的同名函数自动成为虚函数。
转载请注明原文地址:https://kaotiyun.com/show/lMVp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
若变量已正确定义并赋初值,以下合法的赋值语句是()。
若有定义语句doublex,y,*px,*py;,执行了px=&x;py=&y;之后,正确的输入语句是()。
有以下程序: #include<stdio.h> voidf(int*p,int*q); main() { intm=1,n=2,*r=&m; f(r,&n); printf("%d,%d",m,n); } vo
设有以下程序段 main() {intx[M][N]; … Arrlet(x); … } 则作为函数Arrlet的形参,以下表示形式非法的是()。
有以下程序 #include<stdio.h> #include<string.h> main() {charstr[][20]={"One*World","One*Dream!"},*p=str[1]; printf("%d,",st
有下列程序 #include<stdio.h> main() {inta=123456,b; while(a) {b=a%10; a/=10: switch(b) {default:printf("%d",b++);
对于以下函数声明 voidfun(intarray[4],int*ptr);以下叙述中正确的是()。
下面对软件特点描述正确的是()。
以下选项中不能作为C语言合法常量的是()。
随机试题
A、早发性胎心减速B、晚期减速C、变异减速D、NST有反应E、NST无反应临产后,由于子宫收缩时脐带受压,兴奋迷走神经,胎儿监护时可能出现
在胸部,距前正中线6寸的经脉是:
理想盖髓剂应具备性质如下,除外
确定管理幅度应考虑的因素主要是()。
下列关于国家所有权的说法,错误的是()。
授信集中度限额可以按不同维度进行设定,其中()不是其最常用的组合限额设定维度。
测验
下列对事业单位属性表述正确的是()。(常考)
文化遗产
Habitsareafunnything.Wereachforthemmindlessly,settingourbrainsonauto-pilotandrelaxingintotheunconsciouscomfo
最新回复
(
0
)