本文共 560 字,大约阅读时间需要 1 分钟。
定义一个Cat类,拥有静态数据成员numOfCats,记录Cat的个体数目(此题暂定有3只猫);静态成员函数getNumOfCats(),读取numOfCats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #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; } |
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1338331,如需转载请自行联系原作者