有如下程序: #include using name space std; classB{ public: B(int xx):x(xx){++count;x+=10;} virtual void show()const {cout<<count<<'_'<<x<<endl;} protected: static int count; private: intx; }; class D:publicB{ public: D(int xx,int yy):B(xx),y(yy){++count;y+=100

题目

有如下程序: #include using name space std; classB{ public: B(int xx):x(xx){++count;x+=10;} virtual void show()const {cout<<count<<'_'<<x<<endl;} protected: static int count; private: intx; }; class D:publicB{ public: D(int xx,int yy):B(xx),y(yy){++count;y+=100;} virtual void show()const {cout<<count<<'_'<<y<endl}; pnvate: inty; }; int B::count=0; intmain(){ B*ptr=new D(10,20); ptr->show(); delete ptr; return 0; } 运行时的输出结果是( )。

A.1_120

B.2_120

C.1_20

D.2_20


相似考题
更多“有如下程序:#includeusing name space std;classB{public:B(int xx):x(xx){++count;x+=10;}virtu ”相关问题
  • 第1题:

    有如下程序:includeusing namespace std;class XX{protected;int k;public:XX(int n=

    有如下程序: #include<iostream> using namespace std; class XX{ protected; int k; public: XX(int n=5):k(n){} ~XX(){cout<<"XX";} Virtual void f()cons=0; }; inline void XX::f()const{cout<<k+3;} class YY:public XX{ public:

    A.28XX

    B.28YYXX

    C.-33XX

    D.-33XXYY


    正确答案:A

  • 第2题:

    有如下程序:includeusing namespace std;class Toy{public:Toy(char*_n){strcpy(name

    有如下程序: #include<iostream> using namespace std; class Toy{ public: Toy(char*_n){strcpy(name,_n);count++;} ~Toy(){count--;} char*GetName( ){return name;} static int getCount( ){return count;} private: char name[10]; static int count; }; int Toy::count=0: int main( ){ Toy tl("Snoopy"),t2("Mickey"),t3("Barbie"); cout<<t1.getCount( )<<endl; return 0; } 程序的输出结果是

    A.1

    B.2

    C.3

    D.运行时出错


    正确答案:C
    解析:静态数据成员是同一个类的不同对象之间的数据共享,无论创建多少个类,均只有一个静态数据成员,通过对静态数据成员的调用,实现了数据共享。本题创建了3个Toy对象,所以调用3次构造函数,count被增加3次,故sount=3。

  • 第3题:

    有如下程序:includeusingnamespacestd;classXX{protected: intk;public: XX(intn=5):

    有如下程序: #include <iostream> using namespace std; class XX { protected: int k; public: XX(int n=5):k(n){} ~XX() { cout<<"XX"; } virtual void f() const=0; }; inline void XX::f()

    A.28XX

    B.28YYXX

    C.-33XX

    D.-33XXYY


    正确答案:A
    解析:本题中,&p是一个对象指针,通过使用new运算符变成了指向派生类YY的对象指针。所以当建立并初始化对象指针&p时,程序将调用基类XX的构造函数,给私有数据成员k赋值5。然后调用派生类YY的常成员函数f(),输出值2和8。最后调用基类XX的析构函数输出XX。

  • 第4题:

    下列程序的执行结果是______。 include class Student { public: Student(int xx){x=

    下列程序的执行结果是______。

    include<iostream.h>

    class Student

    {

    public:

    Student(int xx){x=xx;}

    virtual float calcTuition( );

    protected:

    int x;

    };

    float Studertt::calcTuition( )

    {

    return float(x*x);

    }

    class GraduateStudent:public Student

    {

    public:

    GraduateStudent(int xx):Student(xx){}

    float calcTuition( );

    };

    float Graduatestudent::calcTuition( )

    {

    return float(x*2);

    }

    void main( )

    {

    Student s(20);

    GraduateStudent gs(30);

    cout<<s.calcTuition( )<<" "<<gs.calcTuition( )<<endl;

    //计算学生s和研究生gs的学费

    }


    正确答案:400 60
    400 60

  • 第5题:

    有如下程序: #include<iostream> usingnamespacestd; classB{ public: B(intxx):x(xx){++count;x+=10;} virtualvoidshow( )const {cout<<count<<<<x<<endl;} protected: staticintcount; private: intx; }; classD:publicB{ public: D(intxx,intyy):B(XX),y(yy){++count;y+= 100;) virtualvoidshow( )const {cout<<count<<<<y<<endl); private: inty; }; intB::count==0; intmain( ){ B*ptr=newD(10,20); ptr->show( ); deleteptr; return0; } 运行时的输出结果是( )。

    A.1_120

    B.2_120

    C.1_20

    D.2_20


    正确答案:B
    B。【解析】本题考查了类的继承。继承有3种方式,public公有、private私有和protected保护,本题都涉及了。本题中类D公有继承类B。在类B中又定义了虚函数,并且有保护类静态类型count及私有变量x。主函数中调用类D,类D又继承了类B,经过系统及调用,本题最终结果为2_120。