单选题已知函数print()没有返回值,如果在类中将之声明为常成员函数,正确的是(  )。A void print()const;B const void print();C void const print();D void print(const);

题目
单选题
已知函数print()没有返回值,如果在类中将之声明为常成员函数,正确的是(  )。
A

void print()const;

B

const void print();

C

void const print();

D

void print(const);

参考答案和解析
正确答案: A
解析:
const成员函数说明格式如下:<返回类型><成员函数名>(<参数表>)const。
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

有以下类定义 classPoint{ public: Point(int x=0,int y=0){_x=x;_y=y;} void Move (int xOff,int yOff {_x +=xOff;_y+yOff} void Print() const {cout<<'('<<_x<<','<<_y<<')'<<endl;} private: int_x_y; }; 下列语句中会发生编译错误的是

A.Pointpt;pt;Print();

B.const Point pt;pt.Print();

C.Pointpt;pt.Move(1,2);

D.const Point pt;pt.Move(1,2);


正确答案:D
解析:本题考核常对象、常数据成员与常成员函数。如果将一个对象说明为常对象,则通过该常对象只能调用它的常成员函数,不能调用其他的成员函数,D选项中对象 pt为常对象,而成员函数Move()不是常成员函数,所以这样调用会发生编译错误。

第2题:

有如下程序: include using namespace std; class Monitor{ public: Monitor(cha

有如下程序:

include<iostream>

using namespace std;

class Monitor{

public:

Monitor(char t):type(t){ }

void Print( )const

{cout<<"The type of monitor is"<<type< private:

char type;

};

class Computer{

public:

Computer(int i,char C) :______{}

void Print( )const

{eout<<"The computer is"<<id<<endl;mort.Print( );}

private:

int id;

Monitor mon;

};

int main( ){

const Computer myComputer(101,'B');

myComputer.Print( );

return 0;

}

请将程序补充完整,使程序在运行时输出:

The computer is 101

The type of monitor is B


正确答案:id(I)mon(C)
id(I),mon(C) 解析:带参构造函数的定义格式(在类外部声明)为:
类名::构造函数名([参数表]):数据成员名1(初始值1),数据成员名2(初始值2)……在类中声明为:
构造函数名([参数表]):数据成员名1(初始值1),数据成员名2(初始值2)……
在compute中有两个数据成员,所以在构造函数中应该对这两个数据成员id和mon初始化,初始化mon创建一个对象,参数为构造函数的形参c。

第3题:

已知:print( )函数是一个类的常成员函数,且无返回值。在下列表示中,正确的是( )

Avoid print( ) const;

Bconst void print( );

Cvoid print( );

Dvoid print(const);


正确答案:A

第4题:

( 29 )有如下程序

#include <iostream>

using namespace std;

class A{

public:

A(int i=0):r1(i) { }

void print() {cout<< ' E ’ <<r1<< ' - ' ;}

void print() const {cout<< ' C ' <<r1*r1<< ' - ' ;}

void print(int x) {cout << ' P ' <<r1*r1*r1<< ' - ' ;}

private:

int r1;

};

int main() {

A a1;

const A a2(4);

a1.print(2);

a1.print();

return 0;

}

运行时的输出结果是

A ) P8-E4

B ) P8-C16-

C ) P0-E4-

D ) P0-C16-


正确答案:D

第5题:

( 11 )有如下程序:

#include<iostream>

using namespace std;

class Monitor{

public:

Monitor ( char t ) : type ( t ) {}

void print ( ) const

{cout<<"The type of monitor is"<<type<<endl;}

private:

char type;

};

class Computer{

public:

Computer ( int i , char c ) : 【 11 】 {}

void Print () const

{cout<<"The computer is"<<id<<endl;mon.Print ( ) ; }

private:

int id;

Monitor mon;

};

const Computer myComputer ( 101,'B' ) ;

myComputer .Print ( ) ;

return 0;

}

请将程序补充完整,使程序在运行时输出:

The computer is 101

'The type of monitor i.s 8


正确答案:

第6题:

( 15 )下列程序的输出结果是 【 15 】 。

#include

using namespace std;

class A {

int a;

public:

A():a(9){}

virtual void print() const { cout<<a;};

};

class B : public A {

char b;

public:

B( ){b='S';}

void print( ) const { cout <<b;}

};

void show(A &x){ x.print();}

int main()

{ A d1,*p;

B d2;

p=&d2;

d1.print();

d2.print();

p->print();

show(d1);

show(d2);

return 0;}


正确答案:

第7题:

( 28 )有如下程序:

#include<iostream>

using namespace std;

class MyClass{

public:

MyClass(int x):val(x) {}

void Print() const {cout<<"const:val="<<val<<'\t';}

void Print(){cout<<"val="<<val<<'t';}

private:

int val;

};

int main(){

const MyClass obj1(10);

MyClass obj2(20);

obj1.Print();

obj2.Print();

return 0;

}

程序的输出结果是

A ) val=10 const:val=20

B ) const:val=10 const:val=20

C ) const:val=10 val=20

D ) val=10 val=20


正确答案:C

第8题:

下列程序的输出结果是 ( )

#include

using namespace std;

class A{

int a

public:

A():a(9){}

virtual void print() const {cout<};

class B:public A{

char b;

public:

B(){b= ‘S’;}

void print()const{cout<};

void show(Aa&X){X,print()}

int main()

{ Ad1;*p;

Bd2;

p=&d2;

d1,print();

d2,print();

p->print();

show(d1);

show(d2);

return 0;}


正确答案:

s--

第9题:

( 28 )有如下程序

#include <iostream>

using namespace std;

class A {

public:

A(int i):rl(i) {}

void print() {cout<<'e'<<r1<<'-';}

void print() const {cout<<'C'<<rl*rl<<'-';}

private:

int rl;

};

int main(){

A al(2); const A a2(4);

Al.print();a2.print();

Return 0;

}

运行时的输出结果是

A )运行时出错

B ) E2-C16-

C ) C4-C16-

D ) E2-E4-


正确答案:B

第10题:

有如下程序:includeusing namespace std;class MyClass{public:MyClass(int x):val(x

有如下程序: #include<iostream> using namespace std; class MyClass{ public: MyClass(int x):val(x){} void Print()const{cout<<“const:val=”<<<val<<‘\’;} void Print(){cout<<“val=”<<val<<‘t’;} private: int va1; }; int main(){ cons

A.val=10 const:val=20

B.const:val=10 const:val=20

C.const:val=10 val=20

D.val=10 val=20


正确答案:B
解析: 本题考查提派生类中构造函数的定义。派生类的构造首先要调用基类的构造函数,对基类成员初始化;然后对派生类中的新增成员初始化。格式:派生类名(构造函数形参表)基类构造函数(形参表)。

更多相关问题