有如下程序: #include #include using namespace std; class MyBag{ public: MyBag(string b

admin2021-02-22  13

问题 有如下程序:
       #include
       #include
       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("CityLife","Gray"),two("Micky","Red");
           cout<           return 0;
       }
若程序运行时的输出结果为 2,则横线处缺失的语句是

选项 A、intcount=0;
B、staticintcount=0;
C、intMyBag::count=0;
D、staticintMyBag::count=0;

答案C

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

最新回复(0)