首页 > 其他 > 详细

记录Cat类的个体数目

时间:2015-05-22 20:51:19      阅读:318      评论:0      收藏:0      [点我收藏+]
B.记录Cat类的个体数目
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 22 (17 users) Total Accepted: 12 (12 users) Special Judge: No
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);
    }
}

记录Cat类的个体数目

原文:http://www.cnblogs.com/zeross/p/4523065.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!