问题
单项选择题
有如下程序:
#include<iostream>
#include<string>
using namespace std;
class MyBag
public:
MyBag(string br,string cr):brand( br), color(cr)++count;
~MyBag()--count;
static int GetCount() return count;
pnvate:
string brand,color;
static int count;
;
int main()
MyBag one("CityLife","Gray"),two("Micky","Red");
cout<<MyBag::CetCount();
return 0;
若程序运行时的输出结果为2,则横线处缺失的语句是______。
A.int count=0;
B.static int count=0;
C.int MyBag::count=0;
D.static int MyBag::count=0;
答案
参考答案:C
解析: 本题考查构造函数和析构函数,以及静态数据成员,题目中要求输出2,那么定义两个对象时,就执行构造函数,使得静态数据成员++count,得到2,那么count初始化就应该为0,静态数据成员初始化时,只能在类体外进行初始化,一般形式为:
数据类型类型::静态数据成员名=初值
因此本题选C。