Read Program 4:
/*copyright School of Computer and control engineering completion Date: May 6, 2016 Ma Yanyan problem Description: No input Description: No output description: result; */#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;}
Operation Result:
Summary of Knowledge points:
Derived1 and Derived2 both inherit base, and Myderived inherits Derived1 and Derived2, and for base two semantics, base is set to virtual base class, and virtual is added before inheriting mode. Only the constructor of the virtual base class is called by the most derived constructor, and the other base classes of the derived class are ignored for calls to the constructor of the virtual base class, the constructor of base is called, and then it is output sequentially in the order of the constructors.
Tenth week-Read the program