1:单一继承是先调用基类的构造函数,然后调用派生类的构造函数,但多重继承将如何调用构造函数呢?多重继承中的基类构造函数被调用的顺序以派生表中声明的顺序为准。派生表就是多重继承定义中继承方式后面的内容,调用顺序就是按照基类名标识符的前后顺序进行的。
2:代码如下:
// 8.6.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> using namespace std; class CBicycle { public: CBicycle() { cout << "Bicycle Construct" << endl; } CBicycle(int iWeight) { m_iWeight=iWeight; } void Run() { cout << "Bicycle Run" << endl; } protected: int m_iWeight; }; class CAirplane { public: CAirplane() { cout << "Airplane Construct " << endl; }; CAirplane(int iWeight) { m_iWeight=iWeight; } void Fly() { cout << "Airplane Fly " << endl; } protected: int m_iWeight; }; class CAirBicycle : public CBicycle, public CAirplane { public: CAirBicycle() { cout << "CAirBicycle Construct" << endl; } void RunFly() { cout << "Run and Fly" << endl; } }; void main() { CAirBicycle ab; ab.RunFly(); }
运行结果:
时间: 2024-10-11 22:48:26