zl程序教程

您现在的位置是:首页 >  其它

当前栏目

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

函数 析构
2023-09-14 08:56:53 时间

 

 

 

 

#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