有如下程序: include using namespace std; class Monitor{ public: Monitor(cha有如下程序:include<iostream>using namespace std;class Monitor{public:Monitor(char t):type(t){ }void Print( )const{cout<<"The type of monitor is"<<type< private:char type;};class Computer{pub

题目
有如下程序: include using namespace std; class Monitor{ public: Monitor(cha

有如下程序:

include<iostream>

using namespace std;

class Monitor{

public:

Monitor(char t):type(t){ }

void Print( )const

{cout<<"The type of monitor is"<<type< private:

char type;

};

class Computer{

public:

Computer(int i,char C) :______{}

void Print( )const

{eout<<"The computer is"<<id<<endl;mort.Print( );}

private:

int id;

Monitor mon;

};

int main( ){

const Computer myComputer(101,'B');

myComputer.Print( );

return 0;

}

请将程序补充完整,使程序在运行时输出:

The computer is 101

The type of monitor is B


相似考题
更多“有如下程序: include<iostream> using namespace std; class Monitor{ public: Monitor(cha ”相关问题
  • 第1题:

    请将如下程序补充完整,使得输出结果为:bbaa。 include using naluespace std; class

    请将如下程序补充完整,使得输出结果为:bbaa。

    include<iostream>

    using naluespace std;

    class A{

    public:

    ______{eout<<"aa";}

    };

    class B:public A{

    public:

    ~B( ){eont<<"bb";}

    };

    int ulain( ){

    B*P=new B;

    delete P;

    return 0;

    }


    正确答案:~A( )
    ~A( ) 解析:派生类和基类的析构函数调用顺序是先调用派生类的析构函数,然后调用基类的析构函数,打印“bb”说明已经在调用派生类的析构函数,则要继续打印“aa”,显然就只有定义在基类的析构函数中去打印,故答案为~A( )。

  • 第2题:

    有以下程序include using namespace std:class Base{private:char c;public:Base(cha

    有以下程序#include <iostream>using namespace std:class Base{private: char c;public: Base(char n) :c(n) {} ~Base ( ) { cout<<c; }}; class Derived : public Base{private: char c;public: Derived(char n):Base (n+1),c(n) {} ~Derived() { cout<<c; }};int main(){ Derived obj('x'); return 0;} 执行后的输出结果是

    A.xy

    B.yx

    C.x

    D.y


    正确答案:A
    解析:本题考核继承与派生中继承基类的数据成员与成员函数。在C++中,由于析构函数不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其顺序与执行构造函数的顺序正好相反.在此题的程序中,在主函数main结束时,派生类Derived对象obj将被删除,所以就会调用对象的析构函数。先调用派生类的析构函数,输出x,然后调用基类的析构函数,输出y。

  • 第3题:

    阅读下列说明和C++代码,填写程序中的空(1)~(6),将解答写入答题纸的对应栏内。
    【说明】
    以下C++代码实现一个简单绘图工具,绘制不同形状以及不同颜色的图形。部分类及其关系如图6-1所示。



    【C++代码】
    #include?#include?using?namespace?std;class?DrawCircle?{??????//绘制圆形,抽象类? ? ? public: (1);//定义参数为?int?radius,?int?x,?inty? ?virtual~DrawCircle()?{?}};class?RedCircle:public?DrawCircle?{????//绘制红色圆形? ? ? ? public: void?drawCircle(intradius,?int?x,?int?y)?{cout?<?drawCircle?=?drawCircle;? }? ?virtual~shape()?{?}? public:? ?virtual?void?draw()?=?0;};class?Circle:public?Shape?{????//圆形? ? private:? ? ?int?x,y,radius;? ? public:? Circle(int?x,inty,int?radius,DrawCircle?*drawCircle)? (3)? {? this->x?=?x;? ?this->y?=?y;? ? this->radius?=?radius; }? ? ? public:? void?draw(){? drawCircle?-> (4); }};int?main(){Shape?*redCirclenew?Circle(100,100,10,????(5)????);//绘制红色圆形? Shape?*greenCircle=new?Circle(100,100,10, (6)??);//绘制绿色圆形redCircle >draw();? ?greenCircle?->draw();? ?return?0;}


    答案:
    解析:
    (6)(1)void drawCircle (int radius,int x,int y)
    (2)DrawCircle*drawCircle
    (3)drawcircle
    (4)drawCircle(radius,x,y)
    (5)new RedCircle()
    (6)new GreenCircle()【解析】
    第一空是填接口里面的方法,在接口的实现里面找,可以发现应该填void drawCircle (int radius,int x,int y)。
    第二空可以根据后面this drawCircle=drawCircle判断,这里应该有一个drawCircle属性,因此应该填)DrawCircle drawCircle。
    第三空这里填drawcircle,用-> drawcircle来引用父类的成员。
    第四空调用drawCircle(radius,x,y)方法。
    第五、六空分别创建一个红色圆形对象和一个绿色圆形对象作为Circle里面的实参。

  • 第4题:

    有如下程序: include using namespaee std; class A{ public: A( ){cout<<"A";}

    有如下程序:

    include<iostream>

    using namespaee std;

    class A{

    public:

    A( ){cout<<"A";}

    ~A( ){cout<<"A";}

    };

    class B{

    A a;

    public:

    B( ){cout<<"B";}

    ~B( )t cout<<"B";}

    };

    int main( ){

    B b;

    return 0;

    }

    程序的输出结果是______。


    正确答案:ABBA
    ABBA

  • 第5题:

    有如下程序:include using namespace std;class Base{private:charc;public:Base(cha

    有如下程序:#include <iostream>using namespace std;class Base{private: char c;public: Base(char n) :c(n){} ~Base() { cout<<c; } };class Derived: public Base{private: char c; public: Derived(char n):Base(n+1),c(n) {} ~Derived() { cout<<c; }};int main (){ Derived obj ('x'); return 0;}执行上面的程序净输出

    A.xy

    B.yx

    C.x

    D.y


    正确答案:A
    解析:在C++中,由于析构函数不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其顺序与执行构造函数的顺序正好相反。在此题的程序中,在主函数main结束时,派生类Derived对象obj将被删除,所以就会调用对象的析构函数。先调用派生类的析构函数,输出x,然后调用基类的析构函数,输出y。