#include<iostream> using namespace std; class Human { private: int *i; public: Human(){cout<<"构造函数执行中..."<<endl; i=new int(999);} ~Human(){cout<<"析构函数执行中..."<<endl; delete i;} };
int main() { Human Jack; return 0; }
2.2 输出结果
1 2
构造函数执行中... 析构函数执行中...
三 堆中对象的删除
3.1 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream> using namespace std; class Human { private: int *i; public: Human(){cout<<"构造函数执行中..."<<endl; i=new int(999);} ~Human(){cout<<"析构函数执行中..."<<endl; delete i;} };
int main() { Human *Jack=new Human; delete Jack; return 0; }