24学时攻克C++光盘源代码
深入浅出
方便实用
源代码在线查看: newarray.cpp
// Listing 15.5 - An array on the heap
#include
class CAT
{
public:
CAT() { itsAge = 1; itsWeight=5; } // default constructor
~CAT(); // destructor
int GetAge() const { return itsAge; }
int GetWeight() const { return itsWeight; }
void SetAge(int age) { itsAge = age; }
private:
int itsAge;
int itsWeight;
};
CAT :: ~CAT()
{
// std::cout }
int main()
{
CAT * Family = new CAT[500];
int i;
CAT * pCat;
for (i = 0; i < 500; i++)
{
pCat = new CAT;
pCat->SetAge(2*i +1);
Family[i] = *pCat;
delete pCat;
}
for (i = 0; i < 500; i++)
std::cout
delete [] Family;
return 0;
}