您的位置:首页 > 编程语言 > C语言/C++

静态数据成员

2013-12-09 13:53 190 查看
定义一个Cat类,拥有静态数据成员numOfCats,记录Cat的个体数目(此题暂定有3只猫);静态成员函数getNumOfCats(),读取numOfCats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法。

#include<iostream>
using namespace std;
class Cat{
public:
Cat(){
}
static int getNumOfCats(){
numOfCats++;
cout<<"There are "<<numOfCats<<" cats alive!"<<endl;
}
private:
static int numOfCats;
};
int Cat::numOfCats=0;
int main(){
Cat cat;
cat.getNumOfCats();
cat.getNumOfCats();
cat.getNumOfCats();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息