c ++在多重继承中调用超级构造函数

时间:2014-04-01 18:17:22

标签: c++ multiple-inheritance

代码:

#include <iostream>
#include <string>
using namespace std;

class Mammal {
    public:
        int age;
        Mammal() {age = 55;}

        void say() {
            cout << "I'm a Mammal and my age " << age;
        }
};
class Reptile {
    public:
        int age;
        Reptile() {age = 77;}

        void say() {
            cout << "I'm a Reptile and my age " << age;
        }
};

class Platypus: public Mammal, public Reptile {
    public:
        Platypus() :
            Mammal(), Reptile() {
            cout << "Constructed!" << endl;
        }
};

int main() {
    Platypus p;
    p.Mammal::say();
    p.Reptile::say();
}

结果是:

I'm a Mammal and my age 4215460
I'm a Reptile and my age 4215472

为什么: 1. Platypus构造函数未被调用,&#34;构造函数&#34;没有输出? 2.父母中的age仍然是随机的(父母的构造者也没被称为?)

0 个答案:

没有答案