第十周第十一周阅读程序-4

/*copyright(c)2016.烟台大学计算机学院
 * All rights reserved,
 * 文件名称:text.Cpp
 * 作者:刘涛
 * 完成日期:2016年5月7日
 * 版本号:vc++6.0
 * 问题描述:阅读程序
 */
#include<iostream>
using namespace std;
class Base
{
public:
    Base(char i)
    {
        cout<<"Base constructor.--"<<i<<endl;
    }
};
class Derived1:virtual public Base
{
public:
    Derived1(char i,char j):Base(i)
    {
        cout<<"Derived constructor.--"<<j<<endl;
    }
};
class Derived2:virtual public Base
{
public:
    Derived2(char i,char j):Base(i)
    {
        cout<<"Derived2 constructor.--"<<j<<endl;
    }
};
class MyDerived:public Derived1,public Derived2
{
public:
    MyDerived(char i,char j,char k,char l,char m,char n,char x):
    Derived2(i,j),Derived1(k,l),Base(m),d(n)
    {
        cout<<"MyDerived constructor.--"<<x<<endl;
    }
private:
    Base d;
};
int main()
{
    MyDerived obj('A','B','C','D','E','F','G');
    return 0;
}
运行结果:<span style="font-family: Arial; font-size: 14px; line-height: 26px;"> Base constructor.--E  ①Base为虚基类,调用MyDerived的时候,先初始化最开始的基类,而且只初始化一次,所以为E</span>
<span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;"> Derived1 constructor.--D  ②对derived1进行初始化</span>
</span>
<span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;">Derived2 constructor.--B  ③对derived2进行初始化</span><br style="font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="font-family: Arial; font-size: 14px; line-height: 26px;"> Base constructor.--F ④对MyDerived中的Base数据成员d进行初始化,第五个F。</span></span></span>
<span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;"> MyDerived constructor.--G  ⑤最后执行Myderived构造函数中的输出语句</span>
</span></span>

你可能感兴趣的:(第十周第十一周阅读程序-4)