首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有以下程序 #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
28
问题
有以下程序
#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://kaotiyun.com/show/ZOkp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
给定程序MODI1.C中函数fun的功能是:根据整型形参m,计算如下公式的值。例如,若m=2000,则应输出:0.000160。请改正程序中的语法错误,使它能计算出正确的结果。注意:不要改动main函数,不得增行或删行,也不
有以下程序#include<stdio.h>voidfun(inta,intb,intc){a=b;b=c;c=a;}main(){inta=10
下列叙述中错误的是()。
数据结构中,与所使用的计算机无关的是数据的()。
下列选项中,定义C语言数组的正确语句是()。
使用VC++2010打开考生文件夹下blank1中的解决方案。此解决方案的项目中包含一个源程序文件blank1.c。在此程序中,函数fun的功能是:将形参s所指字符串中的所有数字字符顺序前移,其他字符顺序后移,处理后新字符串的首地址作为函数值返回。例如,
函数fun的功能是:逆置数组元素中的值。例如:若a所指数组中的数据依次为:1、2、3、4、5、6、7、8、9,则逆置后依次为:9、8、7、6、5、4、3、2、1。形参n给出数组中数据的个数。请在程序的下划线处填入正确的内容并把下划线删除,使程序得
开发软件所需高成本和产品的低质量之间有着尖锐的矛盾,这种现象称做()。
开发软件所需高成本和产品的低质量之间有着尖锐的矛盾,这种现象称做()。
运算符函数调用格式的表达式:y/x++与表达式:y.operator/(operator++(x,O))含义相同,由此可看出()。
随机试题
女性,45岁,双手腕关节及掌指关节肿痛3个月,无明显的外伤史,晨起后双手关节僵硬、握拳不紧
A.算术均数B.几何均数C.中位数D.标准差E.全距
自然失业率等于()之和。
新建或新增加生产能力的工程项目,在计算联合试运转费时需考虑的费用支出项目有()。
物业管理工作的评价应着重考虑的内容包括()
海宁观潮习俗沿袭千年,经久不衰,发展至今已形成“一潮三看四景”的追潮旅游,即()。
粮豆类在农田生产期、收获及储藏过程中的各个环节均受到()污染。
设有学生表student(学号,姓名,性别,出生日期,院系)、课程表course(课程号,课程名,学时)和选课表score(学号,课程号,成绩),删除学号为"20091001"且课程号为"C1"的选课记录,正确命令是
十进制整数95转换成无符号二进制整数是____________。
Whatdoesthemanmean?
最新回复
(
0
)