标签:
阅读程序4:
/*copyright 计算机与控制工程学院 完成日期:2016年5月6日 作者:马艳艳 问题描述:无 输入描述:无 输出描述:结果; */ #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; }
知识点总结:
Derived1和Derived2都继承了Base,而MyDerived又同时继承Derived1和Derived2 ,对于Base出现二义性,则将Base设为虚基类,在继承方式前加上virtual,只有最派生的构造函数调用虚基类的构造函数,该派生类的其他基类对虚基类的构造函数的调用会被忽略,则先调用Base的构造函数,然后按构造函数顺序依次输出。
标签:
原文地址:http://blog.csdn.net/qq_33267291/article/details/51330242