zoukankan      html  css  js  c++  java
  • 虚继承

    /*虚继承*/

    #include "stdafx.h"
    #include <iostream.h>

    class furniture
    {
    public:
        furniture( int nWeight )
        {
            m_nWeight = nWeight;
            cout << "
    家具的构造" << endl;
        }
        int m_nWeight;
    };

    //
    虚继承
    //
    使派生类对象只有一份基类(furniture)的拷贝
    //
    在菱形继承下,在上面的两个继承关系中,加上虚继承

    class Safa : virtual public furniture
    {
    public:
        Safa( int nWeight ):furniture(nWeight)
        {
            cout << "
    沙发的构造" << endl;
        }
        void sit()
        {
           
        }
    };

    class Bed : virtual public furniture
    {
    public:
        Bed( int nWeight ):furniture(nWeight)
        {
            cout << "
    床的构造" << endl;
        }
        void sleep()
        {
           
        }
    };

    //
    构造顺序,先是虚基类
    //
    在是非虚基类,按声明的顺序构造
    class SafaBed :public Bed,public Safa
    {
    public:
        SafaBed( int nWeight ): Safa(nWeight),Bed(nWeight),furniture(nWeight)
        {
            Bed::m_nWeight = nWeight;
            cout << "
    沙发床的构造" << endl;
        }
    };

    int main(int argc, char* argv[])
    {
        SafaBed theSafaBed(100);   
        furniture* pFurniture = (Bed*)&theSafaBed;   
        theSafaBed.sit();   
        theSafaBed.sleep();   
        cout << sizeof(theSafaBed) << endl;
       
        return 0;
    }

  • 相关阅读:
    wx小程序用canvas生成图片流程与注意事项
    mysql导入导出csv
    机房测速
    python 后台服务
    python获取硬件信息模块
    nagios外部命令接口
    nginx下的nagios pnp4nagios
    supervisor运行virtualenv环境下的nagios-api
    check_mk通用应用检测插件
    pnp4nagios 性能调优
  • 原文地址:https://www.cnblogs.com/w413133157/p/1660170.html
Copyright © 2011-2022 走看看