1.定义一个Cat类,拥有静态数据成员函数numOfCats,记录Cat的个体数目;静态函成员函数getNumOfCats(),读取numOfCats。设计程序测试这个类。
#include
using namespace std;
class Cat{
public:
Cat(int weight=0):weight(weight){numOfCats++;}
Cat(Cat &p){
weight=p.weight;
numOfCats++;}
~Cat(){numOfCats–;}
int getWeight(){return weight;}
static int showNumOfCats(){cout<<numOfCats<<endl;}
private:
int weight;
static int numOfCats;
};
int Cat::numOfCats=0;
int main()
{
Cat a(100);
Cat b(50);
Cat::showNumOfCats();
return 0;
}