有如下程序:include using namespace std;class Base{protected:int i;public:int有如下程序: #include <iostream.h> using namespace std; class Base { protected: int i; public: int j; }; class Derived:public Base { int m; public: int n; }; int main() { Derived d; d.i=0; /

题目
有如下程序:include using namespace std;class Base{protected:int i;public:int

有如下程序: #include <iostream.h> using namespace std; class Base { protected: int i; public: int j; }; class Derived:public Base { int m; public: int n; }; int main() { Derived d; d.i=0; //[1] d.j=0; //[2] d.m=0; //[3] d.n=0; //[4] return 0; } 其中主函数中有两个赋值语句有错,这两个错误的赋值语句是( )。

A.[1]和 [2]

B.[1]和[3]

C.[2]和[3]

D.[2]和[4]


相似考题
更多“有如下程序:#include <iostream.h>using namespace std;class Base{protected:int i;public:int ”相关问题
  • 第1题:

    下面程序的输出结果是【】。include using namespace std; class base { protected: int

    下面程序的输出结果是【 】。

    include <iostream>

    using namespace std;

    class base

    {

    protected:

    int a;

    public:

    base(){cout<<"0":}

    };

    class basel: virtual public base

    {

    public:

    base1(){ cout<<"1";}

    };

    class base2 : virtual public base

    {

    public:

    base2(){cout<<"2";}

    };

    class derived : public base1,public base2

    {

    public:

    derived () {cout<<"3"; }

    }

    int main ()

    {

    derived obj;

    cout<<end1;

    return 0;

    }


    正确答案:0123
    0123 解析:本题考核含有虚基类的继承中构造函数的调用顺序,应该先调用基类的构造函数,接着是按照派生类继承列表的顺序依次调用虚基类的构造函数,最有调用派生类自己的构造函数.题中先调用base的构造函数,然后调用base1、base2的构造函数,最后调用derived的构造函数。

  • 第2题:

    下面程序的运行结果为includeclass A{ int num;public: A(int){num=i;} A(ABm){num

    下面程序的运行结果为 #include<iostream.h> class A { int num; public: A(int){num=i;} A(ABm){num=a.num++;} void print(){cout<<num;} }; void main() { Aa(1),b(a); a.print(); b.print(); }

    A.11

    B.12

    C.21

    D.22


    正确答案:C
    解析:本题;号查考生对拷贝构造函数的掌握。因为++运算是右结合的,所以在使用a对b赋值时,b的num变为l而a的num变为2(先赋值再自加)。

  • 第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题:

    下面程序的运行结果是includeclass base{protected:int a;public:base( ){cout<<"0

    下面程序的运行结果是 #include<iostream.h> class base{ protected: int a; public: base( ){cout<<"0";} }; class basel:virtual base { public: base1( ){cout<<"1";} }; class base2:virtual base{ public:

    A.123

    B.3120

    C.312

    D.3012


    正确答案:A
    解析:本题考查的是含有虚基类的继承中构造函数的调用顺序,应该先调用基类的构造函数,接着是按照派生类继承列表的顺序依次调用虚基类的构造函数,最后调用派生类自己的构造函数。

  • 第5题:

    下面程序的运行结果是includeclass base{protected: int a;public: base(){cout <<

    下面程序的运行结果是 #include<iostream.h> class base{ protected: int a; public: base(){cout <<"0";} }; class basel:virtual base{ public: basel () {cout <<"1";} }; class base2:virtual base{ public: base2(){cout <<"2";} }; class derived:public basel,public base2{ public: derived(){cout <<"3";} }; void main() { derived obj; cout <<end1; }

    A.123

    B.3120

    C.312

    D.3012


    正确答案:A
    解析:本题考查的是含有虚基类的继承中构造函数的调用顺序,应该先调用基类的构造函数,接着是按照派生类继承列表的顺序依次调用虚基类的构造函数,最后调用派生类自己的构造函数。