B.记录Cat类的个体数目 | |||||
|
|||||
Description | |||||
定义一个Cat类,拥有静态数据成员HowManyCats,记录Cat的个体数目;静态成员函数GetHowMany(),存取HowManyCats。设计程序测试这个类。 |
|||||
Input | |||||
输入整数n,n代表最大的Cat数量。 |
|||||
Output | |||||
分行显示HowManyCats值的变化过程。从1..n及从n-1..0的过程。 | |||||
Sample Input | |||||
5 |
|||||
Sample Output | |||||
There are 1 cats alive! There are 2 cats alive! There are 3 cats alive! There are 4 cats alive! There are 5 cats alive! There are 4 cats alive! There are 3 cats alive! There are 2 cats alive! There are 1 cats alive! There are 0 cats alive! |
#include<iostream>
using namespace std;
class cat
{
public: void getHowMany(int a);
static int HowManyCats;
};
void cat::getHowMany(int a)
{
cout<<"There are "<<a<<" cats alive!"<<endl;
}
int cat::HowManyCats=0;
int main()
{
cat cat1;
int n;
cin>>n;
while(cat1.HowManyCats<n)
{
cat1.HowManyCats++;
cat1.getHowMany(cat1.HowManyCats);
}
while(cat1.HowManyCats>0)
{
cat1.HowManyCats--;
cat1.getHowMany(cat1.HowManyCats);
}
}