有如下程序: #include<iostream> using namespace std; class GA{ public; virtual int f(){return 1;} }; class

admin2017-04-25  12

问题 有如下程序:
    #include<iostream>
    using namespace std;
    class GA{
    public;
    virtual int f(){return 1;}
    };
    class GB:public GA{
    public;
    virtual int f(){return 2;}
    };
    void show(GA g){cout<<g.f();}
    void display(GA&g){cout<<g.f();}
    int main(){
    GA a:show(a);display(a);
    GB b:show(b);display(b);
    return 0;
    }
    运行时的输出结果是(    )。

选项 A、1111
B、1211
C、1112
D、1212

答案C

解析 此题考查派生类与虚函数的考查。由主函数main入手,其中分别定义了类GA和GB的对象a和b。首先,执行参数a的show函数调用,其中的输出语句为调用GA类中的f()虚函数,返回1。同理display(a)函数输出1。show(b)函数中调用的为GA中的f()函数,display(b)调用GA中f()函数,返回1,display(b)调用GB中的f()函数返回2。所以最后输出为1112。
转载请注明原文地址:https://kaotiyun.com/show/Z2Ap777K
0

最新回复(0)