写出程序运行的结果Public class BasePublic virtual string Hello() {return “Base”;}Public class Sub:BasePublic override string Hello() {return “Sub”;}1. Base b = new Base(); b.Hello;2. Sub s = new Sub(); s.Hello;3. Base b = new Sub (); b.Hello;4. Sub s = new Base();

题目

写出程序运行的结果

Public class Base

Public virtual string Hello() {return “Base”;}

Public class Sub:Base

Public override string Hello() {return “Sub”;}

1. Base b = new Base(); b.Hello;

2. Sub s = new Sub(); s.Hello;

3. Base b = new Sub (); b.Hello;

4. Sub s = new Base(); s.Hello;


相似考题
更多“写出程序运行的结果 Public class Base Public virtual string Hello() {return “Base”;} Pu ”相关问题
  • 第1题:

    请阅读下述代码,写出程序执行的结果

    #include <iostream>

    using namespace std;

    class CBase

    {

    public:

    virtual void print()

    {

    cout<< "base" << endl;

    }

    void DoPrint()

    {

    print();

    }

    };

    class CChild1: public CBase

    {

    public:

    virtual void print()

    {

    cout<< "child1" << endl;

    }

    };

    class CChild2: pu

    {

    public:

    virtual void pri

    {

    cout<< "chil

    }

    };

    void DoPrint(CB

    {

    base->DoPrint

    }

    void main()

    {

    CBase* base =

    CChild1* child

    CChild2* child

    DoPrint(child1

    DoPrint(child2

    DoPrint(base);

    delete base;

    base = child1;

    base->print();

    delete child1;

    delete child2;

    }


    正确答案:

     

  • 第2题:

    有如下程序:include using namespace std;class BASE{public:~BASE() {cout<<"BASE";

    有如下程序: #include <iostream> using namespace std; class BASE{ public: ~BASE() {cout<<"BASE"; } }; class DERIVED: public BASE { public: ~DERIVED() {cout<;"DERIVED"; } }; int main(){DERIVED x; return 0;} 执行后的输出结果是______ 。

    A.BASE

    B.DERIVED

    C.BASEDERIVED

    D.DERIVEDBASE


    正确答案:D
    解析:析构函数的调用顺序是,先调用派生类的析构函数,再调用基类的析构函数。

  • 第3题:

    有如下程序:includeusing namespace std;class BASE{public:~BASE(){cout<<"BASE";}}

    有如下程序: #include<iostream> using namespace std; class BASE{ public: ~BASE(){cout<<"BASE";} }; class DERIVED:public BASE{ public: ~DERIVED(){cout<<"DERIVED";} }; int main(){DERIVED x;return 0;} 执行后的输出结果是( )。

    A.BASE

    B.DERIVED

    C.BASEDERIVED

    D.DERIVEDBASE


    正确答案:D
    解析:此题考查的是派生类的定义和使用。当对象被删除时,派生类的析构函数就被执行。由于析构函数不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其顺序与执行构造函数的顺序正好相反。

  • 第4题:

    有如下程序:includeincludeusing namespace std;class BASE{char c;public

    有如下程序: #include<iostream>#include<iosream> using namespace std; class BASE{ char c; public; BASE(char n):c(n){} virtual ~ BASE(){cout<<c;} }; class DERIVED; public BASE{ char c; public: DERIVED (char n): BASE (n+1)

    A.XY

    B.YX

    C.X

    D.Y


    正确答案:A

  • 第5题:

    有以下程序:include using namespace std;class Base{public:Base(){}virtual void w

    有以下程序: #include <iostream> using namespace std; class Base { public: Base(){} virtual void who() { cout<<"Base Class"<<end1; } ~Base(){} }; class Derivel : public Base { public: void who() { cout<<"Derivel Class"<<end1; } }; class Derive2 : public Base { public: void who () { cout<<"Derive2 Class"<<end1; } }; int main () { Base *p; Derivel obj1; Derive2 obj2; p=&obj1; p=&obj2; p->who ( ); return 0; } 执行程序后的输出结果是( )。

    A.Base Class

    B.Derivel Class

    C.Derive2 Class

    D.程序编译时出错


    正确答案:C
    解析:本题考核虚函数的应用。本题中,先定义了一个基类Base,它含有一个虚成员函数who(),随后定义的类Derivel和Derive2都是基类Base的公有派生类。在主函数中定义了一个指向Base类的指针,它也被允许指向其派生类。在执行过程中,不断改变它所指向的对象,p->who就能调用不同的函数实现。这是因为使用了虚函数,因而进行动态联编。程序最后把指针p指向派生类Derive2的对象,由于函数who()在基类Base中是虚函数,所以系统调用Derive2中的who()函数,最后输出Derive2Class。

  • 第6题:

    阅读下面程序,该程序的运行结果是() 。 class BASE{ char c; public: BASE(char n):c(n){} ~BASE(){cout<<c;} }; class DERIVED : public BASE{ char c; public: DERIVED(char n):BASE(n+1),c(n){} ~DERIVED(){ cout<<c;} }; int main() { DERIVED a('X'); return 0; }

    A.XY

    B.YX

    C.X

    D.Y

    E.XX

    F.YY


    1.0和1.5