单选题
有如下程序: #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;} private: string brand,color; static int count; }; ________ int main(){ MyBag one(”CityLifeIl,”Gray”),two(”Micky”,”Red”); cout<<MyBag::GetCount(); return 0; } 若程序运行时的输出结果为2,则横线处缺失的语句是( )。
【正确答案】
C
【答案解析】解析:本题考查构造函数和析构函数,以及静态数据成员,题目中要求输出2,那么定义两个对象时,就执行构造函数,使得静态数据成员++count,得到2,那么count初始化就应该为0,静态数据成员初始化时,只能在类体外进行初始化,一般形式为: 数据类型类型::静态数据成员名=初值 因此本题选C。