// xigou_example.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class IntArray
{
public:
IntArray(int sz); //构造函数,有参
IntArray(); //构造函数,无参
void displayArraySize();//显示数组信息
~IntArray(); //析构函数
private:
int m_size;
int *m_ptr;
};
int _tmain(int argc, _TCHAR* argv[])
{
IntArray a(20), b;
a.displayArraySize();
b.displayArraySize();
getchar();
return 0;
}
IntArray::IntArray(int sz) //构造函数,有参
{
m_size = sz;
m_ptr =new int[sz];
}
IntArray::IntArray() //构造函数,无参
{
m_size = 10;
m_ptr = new int[10];
}
void IntArray::displayArraySize()//显示数组信息
{
cout << "The size of this array is:"
<< m_size << endl;
}
IntArray::~IntArray() //析构函数
{
cout << "调用析构函数" << m_size << endl;
delete[] m_ptr; //收回额外空间
}
运行结果: