📄 hour15_1.cpp
字号:
// Listing 15.4 - An array of pointers to objects
#include <iostream>
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;
};
int main()
{
CAT * Family[500];
int i;
for (i = 0; i < 500; i++)
{
Family[i] = new CAT; // you can new directly, don't need to use pCat.
Family[i]->SetAge(2*i + 1);
}
for (i = 0; i < 500; i++)
std::cout << "Cat #" << i+1 << ": "
<< Family[i]->GetAge() << std::endl;
for (i = 0; i < 500; i++)
{
delete Family[i];
Family[i] = NULL;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -