下列程序中的重载函数disp( )发生错误,错误原因是【 】。 includeclass Sample {priva下列程序中的重载函数disp( )发生错误,错误原因是【 】。include<iostream, h>class Sample{private:int m;static int sr;public:Sample(int a) {m=a; st+=a; }static void disp(){cout<<m<<end1; }static void disp(Sample input){cout

题目
下列程序中的重载函数disp( )发生错误,错误原因是【 】。 includeclass Sample {priva

下列程序中的重载函数disp( )发生错误,错误原因是【 】。

include<iostream, h>

class Sample

{

private:

int m;

static int sr;

public:

Sample(int a) {m=a; st+=a; }

static void disp(){cout<<m<<end1; }

static void disp(Sample input)

{

cout<<input. m<<end1;

}

};

int Sample: : st=2;

void main()

{

Sample Eirst(2), Second(4)

Sample: :disp( );

Sample: :disp (Second);

}


相似考题
更多“下列程序中的重载函数disp( )发生错误,错误原因是【 】。 include<iostream, h>class Sample {priva ”相关问题
  • 第1题:

    下列程序中的this指针的作用是【 】。include class Sample{int n;static int st;publ

    下列程序中的this指针的作用是【 】。

    include <iostream. h>

    class Sample

    {

    int n;

    static int st;

    public,

    Sample() {}

    Sample(int m) {n=m; st=m+10;}

    void Change(int k) {st=st+k;}

    void AddValue(int m)

    {

    Sample s,

    s. n=n+m;

    *this=s;

    }

    void disp( ) {cout<<"n="<<n<<";st="<<st<<end1;}

    };

    int Sample: :st=0

    void main()

    {

    Sample s1(10),s2(10)

    s1.disp()

    s1.AddValue(5),

    s2.Change(100);

    s1.disp();

    s2.disp()

    }


    正确答案:修改本对象的值
    修改本对象的值 解析:本题巧妙的使用this指针来完成类的不同实例进行的修改自身数据成员的操作。

  • 第2题:

    以下程序的执行结果是【 】。 include class Sample { public: int x: int y; void di

    以下程序的执行结果是【 】。

    include<iostream. h>

    class Sample

    {

    public:

    int x:

    int y;

    void disp()

    {

    cout<<"x="<<x<<",y="<<y<<end1;

    }

    };

    void main()

    {

    int Sample:: ** pc;

    Sample s;

    pc=& Sample: :x;

    s.*pc=10;

    pc:=&Sample: :y;

    s.*pc=20;

    s.disp();

    }


    正确答案:x=10y=20
    x=10,y=20 解析:本题比较特殊,考察域作用符的使用规则。其实际含义是;指针先指向x,然后指向y,并利用该指针分别为x和y赋值。在使用过程中进行了作用域的限定。

  • 第3题:

    如下程序编译时发生错误,错误的原因是show函数实现语句错误,则正确的语句应该为______。

    include<iostream.h>

    class test

    {

    private:

    int hum;

    public:

    test(int);

    void show( );

    };

    test::test(int n){num=n;}

    test::show( ){cout<<num<<endl;}

    void main( )

    {

    test T(10):

    T.show( );

    }


    正确答案:void test::show( ){coutnumendl;}
    void test::show( ){coutnumendl;} 解析:show成员函数的声明和实现不一致,即实现部分应有void修饰符,这样才能编译通过。

  • 第4题:

    下列程序的运行结果是【 】。includeclass Sample{int a;public: Sample(int aa=0) {a

    下列程序的运行结果是【 】。

    include<iostream, h>

    class Sample

    {

    int a;

    public:

    Sample(int aa=0) {a=aa;}

    ~Sample() {cout<<"Sample="<<a<<;}

    class Derived: public Sample

    {

    int b;

    public:

    Derived(int aa=0, int bb=0): Sample(aa) {b=bb;}

    ~De rived() {cout <<"Derived="<<b<<'';}

    void main()

    {

    Derived dl (9)

    }


    正确答案:Derived=0 Sample=9
    Derived=0 Sample=9 解析:本题考察派生类和基类的构造函数,析构函数的执行顺序。

  • 第5题:

    有如下程序:includeusing namespace std;class sample{private:int x,y;public:sampl

    有如下程序: #include<iostream> using namespace std; class sample { private: int x,y; public: sample(int i,int j) { x=i; y=j; } void disp() { cout<<"disp1"<<endl; } void disp()const { cout<<"disp2"<<endl; } }; int main() { const sample a(1,2); a.disp(); return 0; } 该程序运行后的输出结果是( )。

    A.disp1

    B.disp2

    C.disp1 disp2

    D.程序编译时出错


    正确答案:B