有如下类定义: class Person{ public: Person(string s):name(s) { } protected: string name;

admin2015-06-27  35

问题 有如下类定义:
       class Person{
       public:
           Person(string s):name(s) { }
       protected:
           string name;
       };
       class Father:virtual public Person{
       public:
           Father(string s):Person(s) { }
       };
       class Mother:virtual public Person{
       public:
           Mother(string s):Person(s) { }
       };
       class Child:public Father,public Mother,virtual public Person{      
       public:
           Child(string s1,string s2,string s3):Mother(s1),Father(s2),Person(s3) { }
       };
在建立派生类Child的对象时,其基类Father、Mother和Person的构造函数的调用顺序为

选项 A、Father,Mother,Person
B、Mother,Father,Person
C、Person,Father,Mother
D、Father,Person,Mother,Person,Person

答案C

解析 执行派生类构造函数的顺序是:1、调用基类构造函数,2、调用子对象构造函数,3、再执行派生类构造函数,所以本题中先调用基类person构造函数,然后执行father构造函数,最后执行mother构造函数。
转载请注明原文地址:https://kaotiyun.com/show/7nBD777K
0

最新回复(0)