问个c++的问题

来源:百度知道 编辑:UC知道 时间:2024/05/25 00:42:59
#include <iostream>
using namespace std;

class Cat
{
public:
Cat() { itsAge = 1; itsWeight = 5; }
~Cat(){}
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;
Cat * pCat;
for (i = 0; i<500; i++)
{
pCat = new Cat;
pCat->SetAge(2*i+1);
Family[i] = pCat;
}

for (i = 0; i<500; i++)
{
cout << "Cat #" << i+1 << ":";
cout << Family[i]->GetAge() << endl;
}

char response;
cin >> response;
return 0;
}
上面的代码是按书上抄的,按书上的输出是:

Cat #1:1
Cat #2:3
Cat #3:5
(中间省略)
Cat #499:997
Cat #500:999

但是我电脑的输出却是如下:

Cat

你这样问说明还没有看懂程序的意思.

第一个循环是赋值, 第二个循环是输出.两个循环放一块结果的确是一样的. 删掉就不一样了.

再仔细看看吧

敲程序吗,不一定要按书上的照搬,基本上老多都是有错误的,你应该参考书上的用自己的方法写,这样进步就快了,有时间再好好读你的程序