有如下程序: #include<iostream> #include<string> using namespace std; class MyBag{ public: MyBag(string br,str

admin2019-05-22  22

问题 有如下程序:
    #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,则横线处缺失的语句是(    )。

选项 A、int count=0;
B、smile int count=0;
C、int MyBag::count=0;
D、stailc int MyBag::count=0;

答案C

解析 本题考查构造函数和析构函数,以及静态数据成员,题目中要求输出2,那么定义两个对象时,就执行构造函数,使得静态数据成员++count,得到2,那么count初始化就应该为0,静态数据成员初始化时,只能在类体外进行初始化,一般形式为:
    数据类型类型::静态数据成员名=初值
    因此本题选C。
转载请注明原文地址:https://kaotiyun.com/show/Kb8p777K
0

相关试题推荐
最新回复(0)