/* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:d.cpp *作 者:张旺华 *完成日期:2015年6月1日 *版 本 号:v1.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<<"Derived1 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; }
运行结果:
知识点运用及学习心得:
通过这个程序,我们可以了解在执行虚基类继承的时候,首先是构造函数,构造函数是最先执行最底层的构造函数也就是base类,在逐步上升。
时间: 2024-10-29 03:44:33