niusouti.com

有以下程序: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+

题目
有以下程序: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。
更多“有以下程序: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) ; ”相关问题
  • 第1题:

    有以下程序:includeusing namespace std;classA{private: int x;public: A(int a) {x

    有以下程序: #include<iostream> using namespace std; class A { private: int x; public: A(int a) { x=a; } friend class B; }; class B { public: void print(A a) { a.x--; cout<<a, x<<end1; } }; int main () { A a(10); B b; b.print (a) ; return 0; } 程序执行后的输出结果是( )。

    A.9

    B.10

    C.11

    D.12


    正确答案:A
    解析:本题考核友元类的应用。在程序中,类B是类A的友元类,因此,在类B的所有成员函数中均可访问类A的任何成员。在main()中,先定义类A的一个对象a(10)和类B的一个对象b。然后通过对象b调用其成员函数print(),输出对象a的私有成员x的值减1即9。

  • 第2题:

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

    若有以下程序: #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<<end1; } }; 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函数的原型是:voidaddvalue(sample*this,intm);,该函数的第一个参数是执行该类对象的一个指针即this指针。由于这个参数是系统隐含的,所以我们在定义该成员函数时并没有看到这样一个参数。在成员函数的定义体中,可以通过this访问这—参数。上述程序的最后输出结果是15。

  • 第3题:

    有以下程序: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。

  • 第4题:

    有以下程序: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赋给),但不能省略,否则会出现编译错误。

  • 第5题:

    有以下程序: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

  • 第6题:

    有如下程序: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。

  • 第7题:

    有如下程序: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。

  • 第8题:

    有以下程序: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。

  • 第9题:

    有下列程序:includeusing namespace std;class TestClass{private:int x,y;public:Te

    有下列程序: #include<iostream> using namespace std; class TestClass { private: int x,y; public: TestClass (int i,int j) { x=i; y=j; } void print() { cout<<"printl"<<endl; } vo

    A.print1

    B.print2

    C.pfint1 print2

    D.程序编译时出错


    正确答案:B
    解析: 由主函数main入手,定义TestClass型的常对象a,然后调用对象a中的成员函数print()。因为在C++中,如果一个对象被声明为常对象,则不能调用该对象中的非const型的成员函数。所以,这里调用的是对象中的const型成员函数“void print ()const”,输出为print2。

  • 第10题:

    有以下程序:include using namespace std;define PI 3.14class Point{ private:int

    有以下程序: #include <iostream> using namespace std; #define PI 3.14 class Point { private: int x,y; public: Point(int a,int b) { x=a; y=b; } int getx() { return x; }

    A.314

    B.157

    C.78.5

    D.153.86


    正确答案:A
    解析:本题考核派生类的定义和应用。本程序设计了一个点类Point,包含了横、纵两个坐标数据x和y,由它派生出了圆类Circle,并加入了新的数据成员,即一个半径r和一个求圆面积的函数成员area。在主函数main中,首先定义了一个圆Circle类的对象c1,并通过它的构造函数初始化其数据成员。由此可知,其半径r的值为10,所以其面积为PI*10'10=314,即对象c1的函数成员area的返回值为314。

  • 第11题:

    有如下程序: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

  • 第12题:

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

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

    A.n=10

    B.n=5

    C.n=20

    D.n=15


    正确答案:D
    解析:本题考核this指针的应用。上述程序中,sample类的add成员函数中使用了this指针,this指针指向当前对象自身,该成员函数中的语句:this->n=s1.n+s2.n;,用于修改当前对象的数据成员n的值,语句:return(*this);用于返回当前对象自身,即对当前对象进行了修改。对于main()函数调用add成员函数语句“s3.add(s1,s2);”。此时,this指针指向的是对象s3,执行该语句前,s3的数据成员n未赋值,执行完该语句后,就修改了s3的私有成员n的值,使其为15。

  • 第13题:

    以下程序的执行结果是 ( )。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。注意:友元函数不是类的成员函数,在类外定义时不要加上类名及其作用域运算符(::)。友元函数的调用与一般函数的调用的方式和原理一致,可以在程序的任何地方调用它。

  • 第14题:

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

    若有如下程序: #include<iostream> using namespace std; int s=O; 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<<end1; 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题:

    有以下程序:includeusing namespace std;definePl 3.14Class Point{private:int x,y

    有以下程序: #include<iostream> using namespace std; #definePl 3.14 Class Point {private: int x,y; public: Point(int a,intB) {X=a; y:b;} int getx() <return x;} int gety() {return y;}}; class Circle:public Point {pri

    A.314

    B.157

    C.78.5

    D.153.86


    正确答案:A
    解析: 本程序设计了一个点类Point,包含了横,纵两个坐标数据x和y,由它派生出了圃类Circle,并加入了新的数据成员,即一个半径r和一个求圆面积的函数成员area。在主函数main中,首先定义了一个圃Circle类的对象c1,并通过它的构造函数初始化其数据成员。由此可知,其半径r的值为10,所以其面积为PI*10*10=314,即对象c1的函数成员area的返回值为314。

  • 第16题:

    若有以下程序: 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,所以构造函数只能通过初始化列表来初始化它。

  • 第17题:

    有以下程序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。注意:友元函数不是类的成员函数,在类外定义时不要加上类名及其作用域运算符(::)。友元函数的调用与一般函数的调用的方式和原理一致,可以在程序的任何地方调用它。

  • 第18题:

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

    有以下程序: #include <iostream> using namespace std; class sample { private: iht 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<<end1; } }; 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函数的原型是:voidaddvalue(sample*this,intm);,该函数的第一个参数是执行该类对象的一个指针即this指针。由于这个参数是系统隐含的,所以我们在定义该成员函数时并没有看到这样一个参数。在成员函数的定义体中,可以通过this访问这一参数。上述程序的最后输出结果是15。

  • 第19题:

    有以下程序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。注意:友元函数不是类的成员函数,在类外定义时不要加上类名及其作用域运算符(::)。友元函数的调用与一般函数的调用的方式和原理一致,可以在程序的任何地方调用它。

  • 第20题:

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

    有以下程序: #include <iostream> using namespace std; class sample { private: int x; public: void setx(int i) { x=i; } int putx () { return x; } }; int main ( ) { sample *p; sample A[3]; A[0] .setx(5); A[1] .setx (6); A[2] .setx(7); for (int j=0;j<3;j++) { p=&A[j]; cout<<p->putx () <<", "; } cout<<end1; return 0; } 执行后执行结果是( )。

    A.5,6,7,

    B.5,5,5,

    C.6,6,6,

    D.7,7,7,


    正确答案:A
    解析:对象数组是指数组元素为对象的元素,该数组中的每一个元素都是同一个类的对象。程序中,定义了类sample的对象数组A,并调用各个对象的成员函数给其私有数据成员赋值。然后通过for循环将其值输出。

  • 第21题:

    若有如下程序: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

  • 第22题:

    若有如下程序:includeusing 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; int main() { sample a(2),b(5); sample::add(); cout<<s<<endl; return 0; } 程序运行后的输出结果是( )。

    A.2

    B.5

    C.7

    D.3


    正确答案:B

  • 第23题:

    若有以下程序:include using namespace std;class point{private: int x, y;public:

    若有以下程序: #include <iostream> using namespace std; class point { private: int x, y; public: point ( ) { x=0; y=0; } void setpoint(int x1,int y1) { x=x1; y=y1;

    A.12,12

    B.5,5

    C.12,5

    D.5,12


    正确答案:D
    解析:本题考核对象指针的定义与使用。分析程序:程序首先定义一个类point。类point中有两个私有成员,整型变量x和y,还有两个公有成员函数setpoint(intx1,inty1)和dispoint()。函数setpoint()用来设置私有成员x和y的值,而函数dispoint()用来显示私有成员x和y的值。主函数中,首先定义了类point的指针对象p,并申请了内存空间,然后调用对象p中公有成员setpoint给对象p中的私有成员x和y赋值,然后调用成员函数dispoint显示x和y的值。由此可知,最后输出的值应该是5,12。