首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #include using namespace std; class B{ public: virtual void f(){cout
有如下程序: #include using namespace std; class B{ public: virtual void f(){cout
admin
2013-04-11
48
问题
有如下程序:
#include
using namespace std;
class B{
public:
virtual void f(){cout<<“B”;}
void g(){cout<<“B”;}
};
class D:public B{
public:
void f(){cout<<“D”;)
void g(){cout<<“D”;}
};
void h(B*p){p一>f0;p一>g();}
int main(){
D obj;
h(&0bj);
return 0:
}
运行这个程序的输出结果是
选项
A、DD
B、BB
C、BD
D、DB
答案
D
解析
在基类B中定义虚函数f()和成员函数g(),而在派生类D中定义了函数f()和同名函数g()。而函数h中的形参为指向基类的指针变量p,所以在主函数中执行h(&obj)时,会调用派生类的函数f(),输出D,再调用基类的函数gO,输出B。
转载请注明原文地址:https://kaotiyun.com/show/hINp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
与以下程序段等价的是()。while(a){if(b)continue;c;}
下面程序执行后的结果是()。#include<iostream>usingnamespacestd;voidfunc1(inti);voidfunc2(inti);charst[]="hello
在重载运算符函数时,下面运算符必须重载为类的成员函数的是()。
指出下面程序段中的错误,并说明出错原因【】。classLocation{intX,Y=20;protected:intzeroX,zeroY;
以下程序的输出结果是【】。#include<iostream.h>voidmain(){int*p;p=newint;*p=200;cou
下面的______函数头格式不正确。
假定a为一个数组名,则下面的______表示有错误。
下列叙述中正确的是()。
移动输入流文件指针的函数是______。
随机试题
有效中和肝素的药物是
成年妇女骨盆倾斜度的正常值应是
保险是以商业互助的形式,对因各种自然灾害和意外事故造成的损失进行补偿的方式。()
天主教礼拜仪式上所用的大型声乐套曲叫()。
心理学研究的问题包括
A.RiskofScreeningtoYoungerWomenB.InvestigatingtheEffectofScreeningC.EffectsPredictedbyTwoDifferentMod
Manycriticsconsiderthatfarmorestressisplacedonachievementsinathleticsthanintheacademicsphere.We’retoldthati
Colorsaresometimescalledhot,cold,orneutral(中性的).Redandyellowaresaidtobehotcolorsbecausetheymakearoomwarme
Despite/Inspiteoffinancialdifficulties(crisis)。此题考查短语despite/inspiteof“尽管”的表达法。
Forthispart,youareallowed30minutestowriteashortessay.Youshouldstartyouressaywithabriefdescriptionofthepi
最新回复
(
0
)