有如下程序: #include #include using namespace std; class Father{ public: Father(string

admin2021-02-22  12

问题 有如下程序:
       #include
       #include
       using namespace std;
       class Father{
       public:
           Father(string s):name(s) { }
           ~Father() { cout<<’F’; }
       private:
           string name;
       };
       class Mother{
       public:
           Mother(string s):name(s) { }
           ~Mother() { cout<<’M’; }
       private:
           string name;
       };
       class Child:public Mother,public Father{
       public:
           Child(string s1,string s2,string s3,int a):Father(s1),Mother(s2),name(s3),age(a) { }
           ~Child() { cout<<’C’; }
       private:
           string name;
           int age;
       };
       int main(){
           Child son("Zhang","Li","Ming",20);
           return 0;
       }
运行时的输出结果是

选项 A、C
B、CMF
C、CFM
D、20CMF

答案C

解析 执行派生类构造函数的顺序是:1、调用基类构造函数,2、调用子对象构造函数,3、再执行派生类析构函数,4、执行基类的析构函数。所以本题中执行析构函数为派生类的析构函数、Father的析构函数、Mother的析构函数,所以输出CFM,答案C正确。
转载请注明原文地址:https://kaotiyun.com/show/5zfp777K
0

相关试题推荐
最新回复(0)