单选题
C++
题号 #2250868
有如下程序:
#include<iostream>
usingnamespacestd;
classPublication{//出版物类
charname[30];
public:
Publication(char*name=”未知名称”){
~strcpy(this一>name,name);
}
constchar*getName( )const{returnname;}
virtualconstchar*getType( )const{return”未知类型”;}
};
classBook:publicPublication{//书类
public:
Book(char*name):Publication(name){}
virtualconstchar*getType( )const{return"书";}
};
voidshowPublication(Publication&p){
cout<<p.getType( )<<":"<<p.getName( )<<
endl;
}
intmain( ){
Bookbook(”精彩人生”);
showPublication(book);
return0;
}
运行时的输出结果是( )。
参考答案
D
答案解析
D。【解析】因为getType是虚函数,所以P.getType()执行继承类的getType函数,输出“书”;P.getName()执行基类的getName函数,输出“精彩人生”。总共输出“书:精彩人生”,故选D。
你可能感兴趣的试题
A.::运算符不能重载
B.类型转换运算符只能作为成员函数重载
C.将运算符作为非成员函数重载时必须定义为友元
D.重载口运算符应完成“下标访问”操作
A.123
B.***123
234234
234234
C.***123
D.***123
***234234***
***234234***
A.Dewchar[strlen(aa)+1]
B.char[strlen(aa)+1]
C.char[strlen(aa)]
D.newchar[sizeof(aa)-1]