C++继承与派生(原理归纳)(2)

//王二小
class er_xiao : virtual public Boss \
    , virtual public  xiao_er /*其实这里这个可以省去,但是这儿是为了写代码而写代码*/
{
 public :
    er_xiao() {
        cout << "this is er_xiao's constructor !" << endl;
    }
    er_xiao(int i) : \
    Boss(i) , xiao_er(i+1)
    {
        cout << "this is er_xiao's constructor !" \
            << " moneny=" << i << endl;
    }
    virtual ~ er_xiao() {
        cout << "this is er_xiao's xigou function !"<<endl;
    }
    void show() {
        cout << "我是王二小,为坏人带路的王二小 !" << endl;
    }
};

//天朝VIP员工
class VIP_em : virtual public Boss
{

public:
    VIP_em(){
        cout << "this is VIP_em's constructor !" << endl;
    }

VIP_em(int i) : \
    Boss(i)
    {
        cout << "this is VIP_em's constructor !" \
            << " moneny=" << i << endl;
    }
    virtual ~VIP_em() {
        cout << "this is VIP_em's xigou function !" << endl;
    }
    void show() {
        cout << "我是VIP , 我有特权! "<<endl;
    }
};

//熊孩子
class stupid_kid : virtual public VIP_em \
    , virtual public xiao_er , \
    virtual public er_xiao
{
 public:
    stupid_kid() {
        cout << "this is stupid_kid's constructor !" << endl;
    }

stupid_kid(int i) : \
        VIP_em(i) , xiao_er(12) , er_xiao(13),xe(i)
    {
        cout << "this is stupid_kid's constructor !" \
            <<" moneny="<<i<<endl;
    }
    ~stupid_kid() {
      cout << "this is stupid_kid's xigou function !"<<endl;
    }

void show() {
        cout << "我是熊孩子,蜀黍,蜀黍,抱抱!" << endl;
    }
private :
    VIP_em vi;
    xiao_er xe;
    er_xiao ex;
};

int main(){

stupid_kid  st(100);
    //父类的函数被覆盖了
    st.show();
    //如何调用父类,强制是一种。
    ((Boss)st).show();
   
    //stupid_kid *pt = &st;
    //stupid_kid  &sb = st;
    // pt->show();
    //((Boss)sb).show();
    return 0;
}


结果为:

this is Boss's constructor !
this is VIP_em's constructor ! moneny=100
this is xiao_er's constructor ! moneny=12
this is er_xiao's constructor ! moneny=13

-------------这部分为熊孩子的继承部分构造函数

下面是私有变量的构造函数

this is Boss's constructor !
this is VIP_em's constructor !

------私有变量 Vip_em调用无参数的构造函数
this is Boss's constructor ! moneny=100
this is xiao_er's constructor ! moneny=100

------私有变量  xiao_er调用有参数的构造函数

this is Boss's constructor !
this is xiao_er's constructor !

------私有变量 xiao_er调用无参数的构造函数
this is er_xiao's constructor !

this is stupid_kid's constructor ! moneny=100
我是熊孩子,蜀黍,蜀黍,抱抱!
宝剑磨砺,斩魂妖,时光磨砂,魔刃出
this is Boss's xigou function !
this is stupid_kid's xigou function !
this is er_xiao's xigou function !
this is xiao_er's xigou function !
this is Boss's xigou function !
this is xiao_er's xigou function !
this is Boss's xigou function !
this is VIP_em's xigou function !
this is Boss's xigou function !
this is er_xiao's xigou function !
this is xiao_er's xigou function !
this is VIP_em's xigou function !
this is Boss's xigou function !
请按任意键继续. . .

C++继承与派生(原理归纳)

6、 从上述代码可以不难看出,  虚内继承,避免了二义性,仅仅压缩了公有的你虚类继承类。

如果要弄清楚虚拟继承,就得先知道virtual table (vtbl) ----我们说的虚函数表

在内存那块, 会留 下一块连续的内存块,用作vtble存储JMP地址,而vtble里头存的便是virtual function(虚函数)地址,

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/a086b935f2949ec73770d1855fd43b09.html