首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序; #inc1ude<iostream> using namespace std; c1ass Base{ public: void output0 {cout<<1;} virtual void
有如下程序; #inc1ude<iostream> using namespace std; c1ass Base{ public: void output0 {cout<<1;} virtual void
admin
2020-04-07
58
问题
有如下程序;
#inc1ude<iostream>
using namespace std;
c1ass Base{
public:
void output0 {cout<<1;}
virtual void Print0 {cout<<’B’;}
};
c1ass Derived:public Base{
public:
void output(){cout<<2;}
void Print(){cout<<’D’;}
};
int main(){
Base *ptr=new Derived;
ptr—>output();
ptr—>Print();
delete ptr;
retum 0;
}
程序的输出结果是( )。
选项
A、1B
B、1D
C、2B
D、2D
答案
B
解析
此题考查的是虚函数与多态性。在成员函数的声明前面加上virual关键字,即可把函数声明为虚函数;在C++中,一个基类指针可以用于指向它的派生类对象,而且通过这样的指针调用虚函数时,被调用的是该指针实际所指向的对象类的那个重定义版本。本题中,从基类的定义可以看出:output()函数是普通成员函数,而Print()函数是虚函数。所以,通过ptr指针调用的output()是基类中的output(),而通过它调用的Print()则是派生类中的Print()。
转载请注明原文地址:https://kaotiyun.com/show/Mx8p777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下列关于break语句的叙述不正确的是()。
已知枚举类型定义语句为:enumToken{NAME,NUMBER,PLUS=5,MINUS,PRINT=10}:则下列叙述中错误的是()。
下面程序的运行结果是#include<iostream.h>intfun(inta[],intn){intresult=1;for(inti=1;i<n;i++)
下列有关类成员的叙述中,正确的是()。
软件设计包括软件的结构、数据接口和过程设计,其中软件的过程设计是指
参照函数模板的写法,完成非模板函数的定义,语句为【 】。#include<iostream,h>#include<string.h>template<classT>
下列字符串中可以用作C++语言标识符的是
下列对模板的声明中正确的是()。
算法的有穷性是指()。
下面各语句中,能正确进行赋字符串操作的语句是
随机试题
曲线y=ex与其过原点的切线及y轴所围面积为【】
下列关于弹性预算列表法的表述中,正确的有()。
在顺序表(3,6,8,10,11,15,16,18,21,25,30)中,用二分法查找关键码值11,所需的关键码比较次数为______。
将考生文件夹下HUAYUAN文件夹中的ANUM.MEM文件删除。
Sofarwe(discuss)thefirstfivechapters.
Iwasjusttogetup______aknockwasheardonmydoor.
Anunidentifiedwitoncesaid,"Laugh,andtheworldlaughswithyou.Snore,andyousleepalone."Yetsnoringisfarfromalau
A、They’velosttheirsuitcases.B、Theycan’tfindtheirdormitory.C、Theirdormroomshavebeengiventootherstudents.D、They’
Here’showthePacificNorthwestispreparingfor"TheBigOne".It’sthemotherofalldisasterdrillsforwhatcouldbethewo
A、Helpherpreparethepresentation.B、Giveherextratimetofinishthereport.C、Givehersomeadviceondoingthereport.D、H
最新回复
(
0
)