代码:
#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
仍然是随机的(父母的构造者也没被称为?)