更多"虚函数支持多态调用,一个基类的指针可以指向派生类的对象,而且通过这样的"的相关试题:
[单项选择]虚函数支持多态调用,一个基类的指针可以指向派生类的对象,而且通过这样的指针调用虚函数时,被调用的是指针所指的实际对象的虚函数,而非虚函数不支持多态调用。有如下程序:
#include <iostream>
using namespaee std;
class Base
public:
virtual void f( )cout<<"f0+";
void g( )cout<<"gO+";
;
class Derived:public Base
public:
void f( )cout<<"f+";
void g( )cout<<"g+";
;
int main( )
Derived d;
Base*P=&d:
p->f( );p->g( );
return 0;
程序的输出结果是
A. f+g+
B. f0+g+
C. f+g0+
D. f0+g0+
[填空题]如果不使用多态机制,那么通过基类的指针虽然可以指向派生类对象,但是只能访问从基类继承的成员。下列程序没有使用多态机制,其输出结果是______。
#include<iostream>
using namespace std;
class Base
public:
void print( ) cout<<’B’;;
class Derived:public Base
public:
void print( ) cout<<’D’;;
int main( )
Derived*pd=new Derived( )"
Base*pb=pd;
pb->print( );
pd->print( );
delete pd;
return 0;
[填空题]
如果不使用多态机制,那么通过基类的指针虽然可以指向派生类对象,但是只能访问问从基类继承的成员。下列程序没有使用多态机制,其输出结果是 【11】 。
#include <iostream>
using namespace std;
class Base {
public:
void print ( ) {cout << ’B’; }};
class Derived: public Base {
public:
void print ( ) {cout <<’D’;}};
int main( )
{
Derived* pd = new Derived( );
Base* pb = pd;
pb->print( );
pd->print( );
delete pd;
return 0;
}
[单项选择]如果不使用多态机制,那么通过基类的指针虽然可以指向派生类对象,但是只能访问从基类继承的成员。有如下程序,没有使用多态机制。
#include <iostream>
using namespace std;
class Base f
int a,b;
public:
Base(int x,int y)a=x; b=y;
void show( )cout<<a<<','<<b<<endl;
;
class Derived:public Base
int c,d;
public:
Derived(int x,int y,int z,int m):Base(x,y) c=z; d=m;
void show( ) cout<<c<<','<<d<<endl;
;
int main( )
Base B1(50,50), *pb;
Derived D1(10,20,30,40);
pb=&D1;
pb->show( );
return 0;
运行时输出的结果是( )。
A. 10,20
B. 30,40
C. 20,30
D. 50,50
[填空题]如果不使用多态机制,那么通过基数的指针虽然可以指向派生类对象,但是只能访问从基类继承的成员。下列程序没有使用多态机制,其输出结果是【 】。
#include
using namespace std;
class Base{
public:
void print( ){cout<<′B′;}};
class Derived:public Base{
public:
void print( ){cout<<′D′;}}
int main( )
{
Derived*pd=new Derived( );
Base*pb=pd;
pb->print( );
pd->print( );
delete pd;
retum 0:
}
[填空题]如果不能使用多态机制,那么通过基类的指针虽然可以指向派生类对象,但是只能访问从基数继承的成员,下列程序没有使用多态机制,其输出结果是__________。
#include
using namespace std;
class Base{
public:
Void print( ){cout<< ‘B’;}};
class Derived: public Base{
public
void print( ){cout<< ‘D’;}}
int main( )
{
Derived*pd=new Dreived);
Base*pb=pd;
pb->print( );
pd->print( );
delete pd;
return 0;
}
[单项选择]生成派生类对象时,派生类构造函数调用基类构造函数的条件是( )。
A. 无需任何条件
B. 基类中显式定义了构造函数
C. 派生类中显式定义了构造函数
D. 派生类构造函数明确调用了基类构造函数