I. First consider the initialization order of the base class and the derived class and the initialization order of the data members in a class at the time of inheritance
#include <iostream>using namespace Std;class a{public:a (int x=0): m_data (x) { cout<< "Constructor A!!! \ n ";} Private:int m_data;}; Class b{public:b (int x=0,int y=0):d ata (y) { cout<< "Constructor B!!! \ n ";} Private:int data;}; Class C:public a,public b{public:c (int x=0,int y=0,int z=0): A (x), B (y), B (z), A (y) { cout<< "Constructor C!!! \ n ";} Private:int C_data; b b; A;}; void Main () { C B;}
the order of the judgment is a sentence, at the time of inheritance declares the order of the inheriting classes and the order in which the data is declared in the class, which are determined at compile time, and are not related to the order of initialization.
Two. Inheritance of diamond classes in C + +
The following is a diamond class, because the base class will have two versions inherited from derived in the lowest inherited class, resulting in inaccurate data
#include <iostream>using namespace Std;class a{public:a (int x=0): m_data (x) { cout<< "constructor a\n";} void Show () {cout<<m_data<< "\ n";} Private:int m_data;}; Class B:virtual public a{public:b (int x=0): A (x) {cout<< "constructor b\n";} void Show () {Show ();cout<<m_data<< "\ n";} Private:};class c:virtual public a{public:c (int x=0): A (x) {cout<< "constructor c\n";}}; Class D:public B,public c{public: D (int x=0): B (x), C (x) { cout<< "constructor d\n";}}; void Main () {D d (1); <span style= "font-size:18px;" >}</span>
Therefore, the emergence of the virtual base class solves this two different version of the base class data, resulting in the invocation must also specify the invocation declaration
#include <iostream>using namespace Std;class a{public:a (int x=0): m_data (x) {cout<< "constructor a\n";} Virtualvoid Show () {cout<< "A::" <<m_data<< "\ n";} Virtual * RET ()//VC 6.0 does not support this, VS 2008 support actually this function conforms to 3 with, return value, function name, parameter same { cout<< "A:: Ret fuction!!! \ n "; return this;} Virtual ~a ()//In a class that has pointer data, the destructor is designed into a virtual function to reflect the value {cout<< "destructor A!!!";} Private:int m_data;}; Class B:public a{public:b (int x=0,int y=0): A (x), data (y) {cout<< "constructor b\n";} void Show () {cout<< "B::" <<data<< "\ n";} b* ret () {cout<< "B:: Ret fuction!!! \ n "; return this;} ~b () { cout<< "destructor B!!!"; } Private:int data;}; void Main () {a A; b b (n); A=b;a.show (); A.ret ();cout<< "\ n";//The following can be implemented polymorphism is based on the assignment of compatibility rules with pointers and references to implement a-*ptr=&b;ptr->show (); Ptr->show (); A &p=b;p.show (); P.ret ();}
The above simple implementation of the polymorphism of some properties, the back will continue to discuss polymorphism this is only a primer
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + stepping into polymorphism