C++入门经典-例8.6-多重继承的构造顺序

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();
}
View Code

运行结果:

posted @ 2017-09-21 21:31  一串字符串  阅读(433)  评论(0编辑  收藏  举报