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

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

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

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

第2题:

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

第3题:

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

有以下程序:

include <iostream>

using namespace std;

class sample

{

int x;

public:

void setx(int i)

{

x=i;

}

int putx ()

{

return x;

}

};

int main ( )

{

sample *p;

sample A[3];

A[0] .set>:(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;

}

执行后的输出结果是【 】。


正确答案:567
5,6,7 解析:本题考核对象指针与对象数组的应用。主函数中定义对象数组A,然后调用各自的setx()函数赋值,最后通过for循环输出各自内部变量,所以程序最后输出5,6,7。

第4题:

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

第5题:

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

第6题:

有如下程序:include using namespace std;class sample{private:int x,y;public: sam

有如下程序: #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"<<end1; } void disp() const { cout<<"disp2"<<end1; };int main () { const sample a(1,2); a.disp(); return 0; } 该程序运行后的输出结果是

A.disp1

B.disp2

C.disp1 disp2

D.程序编译时出错


正确答案:B
解析:本题考核常对象、常数据和常函数。C++中,在定义常对象时必须进行初始化,而且不能被更新。如果将一个对象说明为常对象,则通过该对象只能调用它的常成员函数。题中,对象a被定义成类sample的常对象,所以通过对象a只能调用其常成员函数disp()。所以程序最后输出disp2。

第7题:

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

第8题:

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

若有以下程序: #include <iostream> using namespace std; class sample { private: int x; public: sample() { } void setx(int i) { x=i; } friend int fun(sample B[],int n) { int m=O; for (int i=O; i<n; i++) { if(B[i].x>m) m=B [i].x; } return m; } }; int main ( ) { sample A[10]; int arr[]={90,87,42,78,97,84,60,55,78,65}; for (int i=O;i<10;i++) A[i]. setx (arr[i]); cout<<fun(A, 10)<<end1; return 0; } 该程序运行后的输出结果是( )。

A.97

B.84

C.90

D.78


正确答案:A
解析:程序中定义了一个类sample,以及类sample的友元函数fun()。在主函数中,通过for循环调用各数组对象中的setx成员函数给各个对象的私有数据成员赋值。而函数fun()的功能是返回各个数组对象中的最大私有成员数据的值。

第9题:

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

第10题:

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

更多相关问题