//析构函数的调用 //在一般情况下,调用析构函数的次序正好与调用构造函数的次序相反 //最先被调用的构造函数,其对应的析构函数最后被调用. #include "stdafx.h" #include <iostream> using namespace std; class CPen { public: CPen(int size=10); ~CPen(); int GetSize(); int SetSize(int size); void Write(); private: int m_size; }; CPen::CPen(int size) { m_size = size; } CPen::~CPen() { cout << "this is destructor called:" << m_size << endl; } void CPen::Write() { cout << "The size of pen is :" << m_size << endl; } int main(int argc, char* argv[]) { CPen pen1; pen1.Write(); CPen pen2(15); pen2.Write(); return 0; }