首页 > 试题广场 >

下列代码中的输出是():classBase{public:B

[单选题]

下列代码中的输出是():

class Base {
public:
Base()
{ cout<<"Constructing Base \n"; }
~Base()
{ cout<<"Destructing Base \n"; }
};
class Derived: public Base {
public:
Derived()
{ cout<<"Constructing Derived \n"; }
~Derived()
{ cout<<"Destructing Derived \n"; }
};

int main(void)
{
Derived *d = new Derived();
Base *b = d;
delete b;
return 0;
}




  • Constructing Base

    Constructing Derived

    Destructing Base



  • Constructing Base

    Constructing Derived

    Destructing Derived

    Destructing Base

  • Constructing Base

    Constructing Derived

    Destructing Base

    Destructing Derived

  • Constructing Derived

    Constructing Base

    Destructing Base

    Destructing Derived

将基类和派生类的析构函数声明为虚函数,会得到 B 选项的运行结果。
发表于 2022-04-18 16:29:51 回复(0)