首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有以下程序 #include <iostream> using namespace std; class Base int a; public: Base(int x){ a=x;} void sh
有以下程序 #include <iostream> using namespace std; class Base int a; public: Base(int x){ a=x;} void sh
admin
2009-02-15
46
问题
有以下程序
#include <iostream>
using namespace std;
class Base
int a;
public:
Base(int x){ a=x;}
void show(){cout<<a; }
};
class Derived: public Base
{
int b;
public:
Derived(int i):Base(i+1),b(i) { }
void show(){ cout<<b; }
};
int main()
{
Base b(5),*pb;
Derived d(1);
pb=&d;
pb->show();
return 0;
}
运行后的打印结果是【 】。
选项
答案
2
解析
本题考核基类指针与派生类指针的使用。本例程序中类Derived是从基类 Base公有继承来的。main()中定义了基类对象b和一个基类指针pb,又定义了派生类Derived的对象d。由于Derived是Base的子类型,因此可以将派生类Derived的对象d的地址赋值给指向基类Base的指针pb,但这时指针pb只能使用从基类Base继承的成员。所以通过对象指针pb调用的show函数是基类的成员函数show(),从而输出基类私有数据成员a的值2。
转载请注明原文地址:https://jikaoti.com/ti/7Yd0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
在黑盒测试方法中,设计测试用例的主要根据是()。
以下关于逻辑运算符两侧运算对象的叙述中正确的是()。
下面的程序在编译时产生错误,其出错原因是()。#include<stdio.h>main(){int1_case;floatprintF;printf(’’请输入2个数:’’);scanf(’
以下选项中正确的定义语句是
使用VC++2010打开考生文件夹下modi1中的解决方案。此解决方案的项目中包含一个源程序文件modi1.C。在此程序中,函数fun()的功能是统计s所指一维数组中0的个数(存在变量zero中)和1的个数(存在变量one中),并输出结果。请改正函数fu
使用VC++2010打开考生文件夹下blank1中的解决方案。此解决方案的项目中包含一个源程序文件blank1.c。在此程序中,函数fun的功能是:将形参s所指字符串中的所有数字字符顺序前移,其他字符顺序后移,处理后新字符串的首地址作为函数值返回。例如,
有3个关系R、S和T如下:则由关系R、S得到关系T的操作是()。
有以下定义语句,编译时会出现编译错误的是
给定程序MODII.C中函数fun的功能是:应用递归算法求形参a的平方根。求平方根的迭代公式如下:例如,a为2时,平方根值为:1.414214。请改正程序中的错误,使它能得出正确结果。注意:不要改动main函数,不得增行或删行,也不得更
给定程序中,函数fun的功能是将参数给定的字符串、整数、浮点数写到文本文件中,再用字符串方式从此文本文件中逐个读入,并调用库函数atoi和atof将字符串转换成相应的整数、浮点数,然后将其显示在屏幕上。请在程序的下划线处填入正确的内容并把下划线删
随机试题
男性,40岁,胸痛、反酸、烧心、嗳气2个月,胃镜检查食管黏膜未见明显异常,最有助于明确诊断的检查是
该患者目前高热最可能的原因是为进一步明确病因检查首选
国际间接投资是指发生在国际资本市场中的投资活动,包括()。
下列单位可以作为“经营单位”填报的是()。
根据《证券投资顾问业务暂行规定》,证券公司、证券投资咨询机构的人员从事证券投资顾问业务时,应当()。Ⅰ.忠实客户利益,切实维护客户合法权益Ⅱ.遵守法律、行政法规的规定Ⅲ.遵循诚实信用原则,勤勉、审慎的为客户提供证券投资顾问服务Ⅳ
某市工商银行2003年取得如下经营业务收入:(1)由上级银行借入外汇资金后贷给国内用户,收取的利息;(2)通过融资租赁方式向承租者收取的租赁费;(3)为客户提供经济服务收取的手续费和代垫、代收代付的邮电费;(4)转让
劳动监察是由法定的专门机关对劳动和社会保障法律法规的实施情况进行的监督检查,这是劳动监察的()属性。
建设社会主义先进文化对公民素质的综合要求是有理想、有道德、有文化、有纪律,它们之间的关系是
数据流图(DFD)中的有向箭头(→)表示
IntroductiontoandRequirementsforPsychologyCourse1.CoursecontentIntroductiontothestudyofthe【T1】【T1】______Topics:
最新回复
(
0
)