下面程序的运行结果是( )。 #include using namespace std; class TestClass { static int n; public: TestClass 0

admin2020-10-21  24

问题 下面程序的运行结果是(    )。
#include
    using namespace std;
    class TestClass
    {
    static int n;
    public:
    TestClass 0
    {
    n++;
    }
    static int test()
    {
    for(int i=0;i<4;i++)
    n++:
    return n:
    }
    };
    int TestClass::n=0;
    int main()
    {
    cout<    TestClass el,c2;
    cout<    return 0;
    }

选项 A、4,10
B、4,6
C、0,6
D、0,4

答案A

解析 在主函数中首先调用TestClass中的test函数输出,类中的n为静态数据成员,可以为所有的对象共享这些数据,这里调用后n等于4。定义对象c1,c2调用构造函数后n=6,所以主函数再次执行“cout<
转载请注明原文地址:https://kaotiyun.com/show/xVyp777K
0

最新回复(0)