有如下类声明:class SAMPLE{int n;public:SAMPLE(int i=0):n(i){}void setValue(int n(0);};下列

题目

有如下类声明:class SAMPLE{ int n; public: SAMPLE(int i=0):n(i){} void setValue(int n(0);};下列关于getValue 成员函数的实现中,正确的是( )。

A.SAMPLE:: setValue(int n0){ n=n0; )

B.void SAMPLE:: setValue(int n0){ n=n0;}

C.void setValue (int n0){ n=n0;}

D.setValue(int n0){ n=n0;}

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

第1题:

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

若有以下程序:

include <iostream>

using namespace std;

class sample

{

int x;

public:

sample(){}

void setx(int i)

{

x=i;

}

friend iht fun(sample B[],int n)

{

int m=0;

for{int i=0;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;

}

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


正确答案:97
97 解析:本题考核对象数组的应用。主函数for循环中利用数组对象的setx()函数给各个对象的私有数据成员赋值。然后通过fun函数返回最大值。

第2题:

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

第3题:

有如下类声明:

class SAMPLE

{

int n;

public:

SAMPLE (int i = 0): n(i){}

void setvalue(int n0);

};

下列关于 setvalue 成员函数的实现中,正确的是

A . SAMPLE::setvalue(int n0){n = n0;}

B . void SAMPLE::setvalue(int n0){n = n0;}

C . void setvalue(int n0){n = n0;}

D . setvalue(int n0){n = n0;}


正确答案:B

 

第4题:

有如下类声明: class TestClass { int n; public: TestClass(int i=0):n(i){} void setValue(int n0); }; 下面关于setValue成员函数的实现中,正确的是( )

A.TestClass::setValue(int n0){n=n0;)

B.void TestClass::setValue(int n0){n=n0;}

C.void setValue(int n0){n=n0;}

D.setValue(int n0){n=n0;}


正确答案:B

第5题:

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

第6题:

以下程序的执行结果是______。 include class Sample { int n; public: Sample() {}

以下程序的执行结果是______。

include<iostream.h>

class Sample

{

int n;

public:

Sample() {}

Sample (int i (n=i;)

void add (Sample &s1,Sample s2)

{

this->n=sl.n+=s2.n;

}

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

}

void main ()


正确答案:n=7 n=5 n=7
n=7 n=5 n=7

第7题:

若有以下程序: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()的功能是返回各个数组对象中的最大私有成员数据的值。

第8题:

有如下类声明; class SAMPLE { int n; public: SAMPLE(int i=0):n(i){} void setValue(int n0); }; 下列关于setValue成员函数的实现中,正确的是( )。

A.SAMPLE::setValue(int n0){n=n0;)

B.void SAMPLE::setValue(int n0){n=n0;}

C.void setValue(int n0){n=n0;}

D.(int n0){n=n0;}


正确答案:B
解析:此题考查了成员函数的定义格式。在类外部对成员函数定义时的一般格式为:返回类型类名::成员函数名(参数表){函数体}。

第9题:

以下程序段共有错误()处include using namespaces std;class Sampleprivate:int n;pu

以下程序段共有错误( )处 #include <iostream> using namespaces std; class Sample private: int n; public: Sample(int i) { n=i; } void setvalue(int i) { n=i; } void display() { cout<<"n="<<n<<end1; } }; int main() { const Sample a(l0); a.setvalue(5); a.display(); return 0; }

A.1

B.2

C.3

D.4


正确答案:B
解析:本题考核常对象、常数据成员和常成员函数。本程序中有两个错误:第1个错误是a.setvalue(5);语句,由于对象a为常对象,所以对象a的私有数据成员n不能被更新;第2处错误是语句a.display();。由于对象a为常对象,所以通过对象a只能调用它的成员函数,而成员函数display()不是常成员函数。

第10题:

以下程序段共有错误()处。include using namespaces std;class Sample{private: int n

以下程序段共有错误( )处。 #include <iostream> using namespaces std; class Sample { private: int n; public: Sample(int i) { n=i; } void setvalue (int i) { n=i; } void display() { cout<<”n=”<<n<<endl; } }; int main() { const Sample a(10); a.setvalue(5); a.display(); return 0; }

A.1

B.2

C.3

D.4


正确答案:B
解析:本题考核常对象、常数据成员和常成员函数。本程序中有两个错误:第1个错误是a.setvalue(5);语句,由于对象a为常对象,所以对象a的私有数据成员n不能被更新;第2处错误是语句a.display();。由于对象a为常对象,所以通过对象a只能调用它的成员函数,而成员函数display()不是常成员函数。

更多相关问题