zoukankan      html  css  js  c++  java
  • 基类构造函数构造的空间再派生类实例中有用吗?

    #include <iostream>
    using namespace std;
    
    class Base
    {
    public:
        Base()
        {
            cout<<"Base contruction"<<endl;
            num = 1;
    
        }
        virtual ~Base()
        {
            cout<<"Base deconstruction"<<endl;
        }
        int num;
    };
    
    class Derived: public Base
    {
    public:
        Derived()
        {
    
            cout<<"Derived contruction "<<endl;
        }
        virtual ~Derived()
        {
            cout<<"Derived deconstruction"<<endl;
        }
    
    };
    
    int main()
    {
        Derived derived;
        std::cout << derived.num;
    
    }

    输出:

    Base contruction
    Derived contruction
    1Derived deconstruction
    Base deconstruction

  • 相关阅读:
    5.9上午
    5.4下午
    5.4上午
    5.2上午
    4.19下午
    4.18上午
    4.17下午
    4.17上午
    4.12下午
    4.13下午
  • 原文地址:https://www.cnblogs.com/hong2016/p/6699858.html
Copyright © 2011-2022 走看看