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

admin2021-02-22  14

问题 有如下程序:
       #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 MyBag::count=0;
       int main(){
           MyBag one("CityLife","Gray"),two("Micky","Red");
           _____________________
           return 0;
       }
若程序运行时的输出结果为 2,则横线处缺失的语句不可能是

选项 A、cout<B、cout<C、cout<D、cout<

答案B

解析 本题考查静态数据成员和静态成员函数,静态成员函数只能使用本类中的静态数据成员,且静态成员函数不归属任何一个对象,而是属于类,题目中要求输出2,那么就要将count输出,题目中定义了两个对象,都对count进行了++,所以调用静态成员函数直接使用类名,B选项正确。
转载请注明原文地址:https://kaotiyun.com/show/0sfp777K
0

最新回复(0)