首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有以下程序 #include <iostream> using namespace std; class Base { int a; public: Base(int x){ a=x; } voi
有以下程序 #include <iostream> using namespace std; class Base { int a; public: Base(int x){ a=x; } voi
admin
2013-02-27
55
问题
有以下程序
#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://www.kaotiyun.com/show/tlVp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有下列程序 #include<stdio.h> #defineN4 intfun(inta[][N]) { inti,y=0; for(i=0;i<N;i+4-) y+=a[i][0]+a[i][N-1]; fo
有下列程序 #include<stdio.h> main() { inta=1,b=1,c=1; if(a--||b--&&--C)printf("%d,%d,%d\n",a,b,c); else printf("%
有以下程序:#include<stdio.h>main(){imm=l,n=2,*p=&m,*q==&n,*r;r=P;p=q;q=r;printf("%d,%d,%d,%d\n",m,n,*P,*q);}程序运行后的输出结果是(
下列关于return语句的叙述中,正确的是()。
有以下程序:#include<stdio.h>voidfun(intp){intd=2:p=d++;printf("%d",p);}main(){inta=1:fun(a);printf("%d\n",a);}程序运行
有以下程序:#include<stdio.h>intk=5;voidf(int*s){s=&k;}main(){intm=3,*p=&m;f(p);printf("%d%d\n",m,*p);}程序的运行结果是(
读取二进制文件的函数调用形式为:fread(buffer,size,count,fp);,其中buffer代表的是()。
数据库设计的四个阶段是:需求分析、______、概念设计和物理设计。
设有基类定义:classCbase{private:inta;protected.intb;public:intc;};派生类采用何种继承方式可以使成员变量b成为自己的私有成员()。
随机试题
A、autumnB、difficultyC、fluteD、supposeB
日本人A与常年在中国作业务代表的美国人保罗在云南世博会旅游时相识,二人于1999年5月1日在美国举行了婚礼,婚后A随保罗在北京定居并申请放弃日本国籍,于2000年5月20日获得批准。之后,其向中国有关部门申请加入中国国籍。2000年5月20日后,A的民事行
下列属于负债类账户的是()。
我国现行预算法关于政府举借债务的规定是,“除法律和国务院另有规定外,()”。
财政收支状况对货币供应量有重要影响,如果财政收支平衡。货币供应量()。
用二维表数据来表示实体及实体之间联系的数据模型为______。
TherobotpresentedtothepubliconFridayinTokyo,JapanWhatdoesRonArkinthinkofWL-16?
Allthesesinfulactsmustbecrackeddownonbyallcountriesandsocieties.
MakingandWritingWordsI.AbriefintroductionA.Makingwordsisconceptuallyanengagingandeffectiveinstructionaltask.B
A、Julyistoofaraway.B、Theofferdoesn’tapplytoJuly.C、TheyhaveafullscheduleinJuly.D、Theydesperatelyneedaholida
最新回复
(
0
)