下列程序的输出结果为( )。 #include<iostream.h> classTestClass { public: TestClass(){val++;} static,intval; }; intTestClass::val=0; voidmain() { TestClasscsl; cout<<csl.val<<""; TestClasscs2; TestClasscs3,cs4; cout<<cs2.val<<endl; }A.O3B.13C.14D.24

题目

下列程序的输出结果为( )。 #include<iostream.h> classTestClass { public: TestClass(){val++;} static,intval; }; intTestClass::val=0; voidmain() { TestClasscsl; cout<<csl.val<<""; TestClasscs2; TestClasscs3,cs4; cout<<cs2.val<<endl; }

A.O3

B.13

C.14

D.24


相似考题
更多“下列程序的输出结果为( )。 #include&lt;iostream.h&gt; classTestClass { public: TestClass( ”相关问题
  • 第1题:

    执行下列程序的输出结果是______: include using namespace std; class TestClass1 {

    执行下列程序的输出结果是______:

    include<iostream>

    using namespace std;

    class TestClass1

    {

    public:

    void fun1(){cout<<"TestClass1\n";}

    virtual void fun2(){cout<<"TestClass1\n";}

    };

    class TestClass2:public TestClass1

    {

    public:

    void fun1(){cout<<"TestClass2\n";}

    void fun2(){cout<<"TestClass2\n";}

    };

    void f(TestClass1&b)<b.fun1();b.fun2();}

    int main()

    {

    TestClass2 obj;

    f(obj);

    return 0;

    }


    正确答案:TestClass1 TestClass2
    TestClass1 TestClass2 解析:主函数中首先定义TestClass2对象obj,然后执行“f(obj);”。“void f(TestClass1& b)(b.fun1);b.fun2();}”语句中fun中参数为TestClass1类型的对象b,其中调用fun1()和fun2()。基类 TestClass1中的fun2()为虚函数,所以派生类中的fun2也是虚函数,所以输出为调用TestClass1中的fun1,TestClass2中的fun2。即结果为TestClass1和TestClass2。

  • 第2题:

    若有以下程序: include using namespace std; class TestClass 1 { public: TestClass

    若有以下程序:

    include<iostream>

    using namespace std;

    class TestClass 1

    {

    public:

    TestClass1()

    {

    X=0;

    }

    int x;

    };

    class TestClass2:virtual public TestClass1

    {

    public:

    TestClass2()

    {

    x=10;

    }

    };

    class TestClass3:virtual public TestClass 1

    {

    public:

    TestClass3()

    {

    x=20;

    }

    };

    class TestClass4:public TestClass2, protected TestClass3

    { };

    int main()

    {

    TestClass4 obj;

    cout<<obj.x<<end1;

    return 0:

    }

    该程序运行后的输出结果是______。


    正确答案:20
    20

  • 第3题:

    若有如下程序:includeusing namespace std;class TestClass{public:void who() {cout

    若有如下程序: #include<iostream> using namespace std; class TestClass { public: void who() {cout<<"TestClass"<<endl;} }; class TestClass1:public TestClass { public: void who(){cout<<"TestClass1"<<endl;} }; int main() { TestClass *p; TcstClass1 obj1; p=&obj1; p->who(); return 0; } 则该程序运行后的输出结果是( )。

    A.TestClass1

    B.TestClass

    C.0

    D.无输出


    正确答案:A
    解析:程序中的TestClas1为TestClass的派生类,主函数main中定义TestClass对象*p,TestClass1对象obj1,然后p引用obj1,执行p->who()则是调用基类中的who函数,输出TcstClass。

  • 第4题:

    下列程序输出结果为: include using namespace std; class TestClass1 { public: Test

    下列程序输出结果为:

    include<iostream>

    using namespace std;

    class TestClass1

    {

    public:

    TestClass1(){}

    TestClass1(int i)

    {

    x1=i;

    }

    void dispa()

    {

    cout<<"x1="<<x1<<",";

    }

    private:

    int x1;

    };

    class TestClass2:public TestClass1

    {

    public:

    TestClass2(){}

    TestClass2(int i):TestClass1(i+10)

    {

    x2=i;

    }

    void dispb()

    {

    dispa();

    cout<<"x2="<<x2<<endl;

    }

    private:

    int x2;

    };

    int main()

    {

    TestClass2 b(2);

    b.dispb();

    return 0;

    }


    正确答案:x1=12x2=2
    x1=12,x2=2 解析:由主函数main入手,首先定义类TestClass2的对象b,成员数据为2。然后调用dispb函数。在dispb中首先执行dispa函数,TestClass2为TestClass1的派生类,“TestClass2(int i):TestClass1(i+10)”所以TestClass1中的x1=12,dispa输出的结果为x1=12。在TestClass1中x2=2,所以dispb中的输出语句“cout”x2=”x2endl;”,输出x2=2。即答案为“x1=12,x2=2”。

  • 第5题:

    下列程序的输出结果是()。 include using namespace std; class TestClass{ static in

    下列程序的输出结果是( )。 #include<iostream> using namespace std; class TestClass{ static int i; public: TestClass(){i++;} ~TestClass(){i--;} static int getVal(){retum i;} }; int TestClass∷i=0; voi

    A.111

    B.121

    C.211

    D.221


    正确答案:D
    解析: 由主函数mian入手,首先定义TestClass对象ob1,调用构造函数后,i等于1。在“int TestClass∷i=0”语句中,i首先被赋值为0,它是静态函数可以被所有对象引用,并且静态成员变量,当它值被改变后,则在各对象中这个数据成员的值都同时改变。主函数中首先调用fun函数,其中又定义了对象ob2,调用gtVal;i等于2,输出。主函数“obj*ob3=newTestClass;coutob3->getVal();”动态分配,i等于2,输出。“delete ob3;”析构函数后,i等于1,

  • 第6题:

    若有以下程序:includeusing namespace std;class TestClass{public:void who(){cout<

    若有以下程序: #include<iostream> using namespace std; class TestClass { public: void who(){cout<<"TestClass"<<endl;} }; class TestClass1:public TestClass { public: void who(){cout<<"TestClass1"<<endl;} }; int main() { TestClass *p; TestClass1 obj1; P=&obj1; P->who(); return 0; 则该程序运行后的输出结果是( )。

    A.TestClass1

    B.TestClass

    C.0

    D.无输出


    正确答案:B