析构函数、虚析构函数、纯虚函数

 

 

 

 

#include<iostream>
using namespace std;

class parent
{
public:
    parent()
    {
        cout << "父类构造" << endl;
    }
    ~parent()
    {
        cout << "父类析构" << endl;
    }
};

class child :public parent
{
public:
    child()
    {
        cout << "子类构造" << endl;
    }
    ~child()
    {
        cout << "子类析构" << endl;
    }
};


int main()
{
    {
        parent* A = new child();
        delete A;
    }
    /*父类构造
      子类构造
      父类析构*/
    system("pause");
    return 0;
}
基类不使用virtual
#include<iostream>
  using namespace std;
  class parent
  {
  public:
      parent()
      {
          cout << "父类构造" << endl;
      }
      virtual ~parent()
      {
          cout << "父类析构" << endl;
      }
  };

  class child :public parent
  {
  public:
      child()
      {
          cout << "子类构造" << endl;
      }
      virtual ~child()
      {
          cout << "子类析构" << endl;
      }
  };
  int main()
  {
      {
          parent* A = new child();
          delete A;
      }
    /*父类构造
      子类构造
      子类析构
      父类析构*/
      system("pause");
      return 0;
  }
基类使用virtual

 

 

 

 

class Box
{
   public:
      // 纯虚函数
      virtual double getVolume() = 0;
   private:
      double length;      // 长度
      double breadth;     // 宽度
      double height;      // 高度
};

 

 

 

 

 

 

 https://www.cnblogs.com/-citywall123/p/12745654.html

 

posted @ 2020-05-13 16:55  知道了呀~  阅读(440)  评论(0编辑  收藏  举报