更多“有如下类定义:class Sample{public:Sample();~Sample();private:static int data;};将静态数据成 ”相关问题
  • 第1题:

    ( 10 )类 Sample 的构造函数将形参 data 赋值给数据成员 data 。请将类定义补充完整。

    class Sample{

    public:

    Sample(int data=0);

    Private:

    Int data;

    };

    Sample::Sample(int data){

    【 10 】

    }


    正确答案:

  • 第2题:

    在下面的类定义中,this指针的用途是______。 include class Sample { int x,y; publi

    在下面的类定义中,this指针的用途是______。

    include<iostream.h>

    class Sample

    {

    int x,y;

    public:

    Sample(int i,int j){x=i;y=j;}

    void assign(Sample sa);

    };

    void Sample::assign(Sample p)

    {

    if(this!=&p)

    {

    x=p.x;

    y=p.y;

    }

    }


    正确答案:防止类对象自己给自己赋值
    防止类对象自己给自己赋值 解析:对象给自己赋值是一种自毁行为,这种行为如果不加以判断,可能造成无意识的破坏。

  • 第3题:

    有以下程序include using namespace std;class sample{private:int x;public:sample(

    有以下程序#include <iostream>using namespace std;class sample{private: int x;public: sample(int a) { x=a; } friend double square(sample s);};double square(sample s){ return s. x*s. x;}int main(){ sample s1(20),s2(30); cout<<square(s2)<<end1; return 0;}执行结果是( )。

    A.20

    B.30

    C.900

    D.400


    正确答案:C

  • 第4题:

    在下面的类定义中,错误的语句是

    class Sample { public: Sample(int val); //①

    ~Sample(): //②

    private: int a=2.5; //③

    Sample(); //④ };

    A.①②③④

    B.②

    C.③

    D.①②③


    正确答案:C
    解析:本题考核类的定义。C++不能类的定义中给数据成员赋初值。

  • 第5题:

    有如下类定义,请将Sample类的拷贝构造函数补充完整。

    class Sample{

    public:

    Sample(){)

    ~Sample(){if(p)delete p;)

    Sample(const Sample& s){

    ______

    }

    void SetData(int data) {p=new int(data);}

    private:

    int*p;

    };


    正确答案:p=new int; p=s.p;
    p=new int; p=s.p; 解析:此题考查的是复制构造函数。复制构造函数定义的一般格式如下:类名::类名(const类名&引用对象名){复制构造函数体)。而在类中的声明部分可省去“类名::”。

  • 第6题:

    有以下程序:includeusing namespace std;class sample{private:int x;public:sample(

    有以下程序: #include<iostream> using namespace std; class sample { private: int x; public: sample(int A) { x=a; friend double square(sample s); }; double square(sample s) { return S.X*S.K; } int main() { sa

    A.20

    B.30

    C.900

    D.400


    正确答案:C
    解析: 本题考查友元函数的应用。程序中函数square是类sample的一个友元函数,它可以直接访问类sam- pie的所有成员。它的功能是返回类sample的私有数据成员x的平方。所以程序的执行结果是900。

  • 第7题:

    若有以下程序: include usingnamespace std; class Sample { private: const int n;

    若有以下程序:

    include <iostream>

    using namespace std;

    class Sample

    {

    private:

    const int n;

    public:

    Sample(int i) :n(i) {)

    void print()

    {

    cout<<"n="<<n<<end1;

    }

    };

    int main()

    {

    sample a(10);

    a.print();

    return 0;

    }

    上述程序运行后的输出结果是【 】。


    正确答案:n=10
    n=10 解析:本题考核常成员数据的应用。类Sample中,定义了一个常数据成员n,所以构造函数只能通过初始化列表来初始化它。

  • 第8题:

    有以下程序include using namespace std; class sample { private: int x; public:

    有以下程序 #include <iostream> using namespace std; class sample { private: int x; public: sample(int a) { x=a; } friend double square(sample s); }; double square(sample s) { return s.x*s.x; } int main() { sample s1 (20),s2(30); cout<<square(s2)<<end1; return 0; } 执行结果是

    A.20

    B.30

    C.900

    D.400


    正确答案:C
    解析:本题考核友元函数的应用。程序中函数square是类sample的一个友元函数,它可以直接访问类sample的所有成员。它的功能是返回类sample的私有数据成员x的平方。所以程序的执行结果是:900。注意:友元函数不是类的成员函数,在类外定义时不要加上类名及其作用域运算符(::)。友元函数的调用与一般函数的调用的方式和原理一致,可以在程序的任何地方调用它。

  • 第9题:

    有以下程序:includeusing namespace std;class sample{private:int x;static int y;

    有以下程序: #include<iostrearn> using namespace std; class sample { private: int x; static int y; public: sample (int A) ; static void print (sample s); }; sample::sample(int A) { x=a; y+=x; }

    A.x=10,y=20

    B.x=20,y=30

    C.x=30,y=20

    D.x=30,y=30


    正确答案:B
    解析:本题考核静态数据成员和静态成员函数的应用。类sample中定义两个私有成员x和y,其中y为静态数据成员。并定义函数print()为静态成员函数。在主函数中,定义对象s1(10)时,通过构造函数使对象s1的私有成员x=10,静态数据成员y=10。定义s2(20)时,通过构造函数使对象s2的私有成员x=20,静态数据成员y=10+20=30。程序最后调用静态成员函数print输出对象s2的私有成员x的值20,对象s1、s2共享的静态数据成员y的值30。

  • 第10题:

    有以下程序includeusing namespace std;class sample{private: int x;public: sample

    有以下程序 #include<iostream> using namespace std; class sample { private: int x; public: sample(int a) { x=a; } friend double square(sample s); }; double square(sample S) { return s.x*s.x; } int main() { sample s1(20),s2(30); cout<<square(s2)<<endl; return 0; } 执行结果是

    A.20

    B.30

    C.900

    D.400


    正确答案:C
    解析:本题考核友元函数的应用。程序中函数square是类sample的一个友元函数,它可以直接访问类sample的所有成员。它的功能是返回类sample的私有数据成员x的平方。所以程序的执行结果是:900。注意:友元函数不是类的成员函数,在类外定义时不要加上类名及其作用域运算符(::)。友元函数的调用与一般函数的调用的方式和原理一致,可以在程序的任何地方调用它。

  • 第11题:

    有以下程序:includeusing namespace std;Class sample{private:int n;public:sample(

    有以下程序: #include<iostream> using namespace std; Class sample { private: int n; public: sample(){} sample(int m) { n=m; } sample add(sample s1,samplc s2) { this-->n=s1.n+s2.n; return(*this); } void disp(

    A.n=10

    B.n=5

    C.n=20

    D.n=15


    正确答案:D
    解析: 本题考查this指针的使用类成员函数add中通过this指针实现私有数据成员n的赋值。

  • 第12题:

    若有如下程序:include using namespaces std;int s=0;class sample {static iht n;pu

    若有如下程序: #include <iostream> using namespaces std; int s=0; class sample { static iht n; public: sample(int i) { n=i; } static void add() { s+=n; } }; int sample::s=0; int main() { sample a(2),b(5); sample::add(); cout<<s<<endl; return 0; } 程序运行后的输出结果是

    A.2

    B.5

    C.7

    D.3


    正确答案:B
    解析:本题考核静态数据成员和静态成员函数的应用。程序中定义一个类sample,它包括一个静态数据成员n和一个静态成员函数add,并在类的构造函数中给类私有静态数据成员n赋值。在主函数main中,定义对象a(2)时,通过构造函数使静态数据成员n的值变为2,在定义对象b(5)时,通过构造函数使静态数据成员n=5(覆盖了前面的n=2),再执行sample::add()使全局变量s=5。

  • 第13题:

    ( 11 )有如下类定义:

    class Sample{

    public:

    Sample();

    ~Sample();

    Private:

    Static int date;

    };

    将静态数据成员 data 初始化为 0 的语句是 【 11 】 。


    正确答案:

  • 第14题:

    若有如下程序:includeusing namespaces std;int s=0;class sample{ static int n;pub

    若有如下程序: #include<iostream> using namespaces std; int s=0; class sample { static int n; public: sample(int i) { n=i; } static void add() { s+=n; } ); int sample::s=0; int main() { sample a(2),b(5); sample::add(); cout<<s<<endl; return 0; } 程序运行后的输出结果是

    A.2

    B.5

    C.7

    D.3


    正确答案:B
    解析:本题考核静态数据成员和静态成员函数的应用;程序中定义一个类sample,它包括一个静态数据成员n和一个静态成员函数add,并在类的构造函数中给类私有静态数据成员n赋值。在主函数main中,定义对象a(2)时,通过构造函数使静态数据成员n的值变为2,在定义对象b(5)时,通过构造函数使静态数据成员n=5(覆盖了前面的n=2),再执行sample::add()使全局变量s=5。

  • 第15题:

    若有如下程序:include using namespaces std;int s=0;class sample{static int n;pub

    若有如下程序:#include <iostream>using namespaces std;int s=0;class sample{static int n;public:sample(int i){ n=i;}static void add(){ s+=n;}};int sample:: n=0;int main(){sample a(2),b(5);sample:: add();cout<<s<<end1;return 0;}程序运行后的输出结果是( )。

    A.2

    B.5

    C.7

    D.3


    正确答案:B

  • 第16题:

    以下程序的执行结果是 ( )。include using namespace std;class sample{private: int

    以下程序的执行结果是 ( )。 #include <iostream> using namespace std; class sample { private: int x; public: sample (int A) { x=a; } friend double square(sample s); }; double square(sample s) {

    A.20

    B.30

    C.900

    D.400


    正确答案:C
    解析:本题考核友元函数的应用。程序中函数square()是类sample的一个友元函数,它可以直接访问类sample的所有成员。它的功能是返回类sample的私有数据成员x的平方。所以程序的执行结果是:900。注意:友元函数不是类的成员函数,在类外定义时不要加上类名及其作用域运算符(::)。友元函数的调用与一般函数的调用的方式和原理一致,可以在程序的任何地方调用它。

  • 第17题:

    下列程序的运行结果是【 】。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 解析:本题考察派生类和基类的构造函数,析构函数的执行顺序。

  • 第18题:

    有以下程序:include using namespace std;int s=0;class sample{ static int n;publi

    有以下程序: #include <iostream> using namespace std; int s=0; class sample { static int n; public: sample (int i) { n=i; } static void add() { s+=n; } }; int sample::n=0;

    A.2

    B.5

    C.7

    D.3


    正确答案:B
    解析:程序中定义对象a(2)时,通过构造函数使静态数据成员n=2,在定义对象 b(5)时,通过构造函数使静态数据成员n=5(覆盖了前面n=2),再执行“sample::add();”使全局变量s=5。注意:本题程序中尽管代码中静态数据成员n的初始化语句“int sample::n=0;”没有意义(因为各对象中的n值由变量i赋给),但不能省略,否则会出现编译错误。

  • 第19题:

    有以下程序:include using namespace std;class sample{private: int x; static int

    有以下程序:#include <iostream>using namespace std;class sample{private: int x; static int y;public: sample(int a); static void print(sample s);};sample:: sample(int a){ x=a; y+=x;}void sample:: print(sample s){ cout<<"x="<<s. x<<",y="<<y<<end1;}int sample:: y=0;int main(){ sample s1(10); sample s2(20); sample:: print(s2); return 0;}程序运行后的输出结果是( )。

    A.x=10,y=20

    B.x=20,y=30

    C.x=30,y=20

    D.x=30,y=30


    正确答案:B

  • 第20题:

    有如下程序:include using namespace std;int s=0;class sample { static int n;publ

    有如下程序: #include <iostream> using namespace std; int s=0; class sample { static int n; public: sample(int i) { n=i; } static void add() { s+=n; } };

    A.2

    B.5

    C.7

    D.3


    正确答案:B
    解析:本题考核静态数据成员和静态成员函数的应用。程序中定义一个类sample,它包括一个静态数据成员n和一个静态成员函数add,并在类的构造函数中给类私有静态数据成员n赋值。在主函数main中,定义对象a(2)时,通过构造函数使静态数据成员n的值变为2,在定义对象b(5)时,通过构造函数使静态数据成员n=5(覆盖了前面的n=2),再执行sample::add()使全局变量s=5。

  • 第21题:

    有如下程序:includeusing namespace std;int s=0;class sample{ static int n;public

    有如下程序: #include<iostream> using namespace std; int s=0; class sample { static int n; public: sample(int i) { n=i; } static void add() { s+=n; } }; int sample::s=0; int main() { sample a(2),b(5); sample::add(); cout<<s<<endl; return 0; } 程序运行后的输出结果是

    A.2

    B.5

    C.7

    D.3


    正确答案:B
    解析:本题考核静态数据成员和静态成员函数的应用。程序中定义一个类sample,它包括一个静态数据成员n和一个静态成员函数add,并在类的构造函数中给类私有静态数据成员n赋值。在主函数main中,定义对象a(2)时,通过构造函数使静态数据成员n的值变为2,在定义对象b(5)时,通过构造函数使静态数据成员n=5(覆盖了前面的n=2),再执行sample::add()使全局变量s=5。

  • 第22题:

    有以下程序:include using namespace std;class sample{private:int n;public:sample

    有以下程序: #include <iostream> using namespace std; class sample { private: int n; public: sample(){} sample(int m) { n=m; } void addvalue(int m) { sample s; s.n=n+m; *this=s; } void disp () { cout<<"n="<<n<<endl; } }; int main() { sample s (10); s.addvalue(5); s.disp(); return 0; } 程序运行后的输出结果是

    A.n=10

    B.n=5

    C.n=15

    D.n=20


    正确答案:C
    解析:本题考核this指针的应用。上述程序中sample类定义了一个addvalue非静态成员函数。addvalue函数的原型是:void addvalue(sample *this,int m);,该函数的第一个参数是执行该类对象的一个指针即this指针。由于这个参数是系统隐含的,所以我们在定义该成员函数时并没有看到这样一个参数。在成员函数的定义体中,可以通过this访问这一参数。上述程序的最后输出结果是15。

  • 第23题:

    若有如下程序:includeusing namespace std;int s=0;class sample{static int n;pubic

    若有如下程序: #include<iostream> using namespace std; int s=0; class sample { static int n; pubic: sample(int i) { n=i; } static void add() { S+=n; } }; int sample::n=O; int main() { sample a(2),b(5); sample::add(); cout<<s<<endl; return 0; } 程序运行后的输出结果是( )。

    A.2

    B.5

    C.7

    D.3


    正确答案:B