C++语言程序设计

单选题已知类IMS中两个成员函数的声明为“void listen()const;”与“void speak();”,另有两个对象的定义为“IMS obj1;”与“const IMS obj2;”,则下列语句中,产生编译错误的是(  )。A obj1.listen(  );B obj1.speak(  );C obj2.listen(  );D obj2.speak(  );

题目
单选题
已知类IMS中两个成员函数的声明为“void listen()const;”与“void speak();”,另有两个对象的定义为“IMS obj1;”与“const IMS obj2;”,则下列语句中,产生编译错误的是(  )。
A

obj1.listen(  );

B

obj1.speak(  );

C

obj2.listen(  );

D

obj2.speak(  );

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

第1题:

( 12 )有如下程序:

#include <iostream>

using namespace std

class Animal{

public:

virtual char* getType () const { return "Animal" ; }

virtual char* getVoice () const { return "Voice" ; }

};

Class Dog : public Animal {

public:

char* getType ( ) const {return "Dog" ; }

char* getVoice ( ) const {return "Woof"}

};

void type ( Animal& a ) {cout<<a.getType ( ) ; }

void speak ( Animal a ) {cout<<a.getVoice ( ) ; }

int main ( ) {

Dog d; type ( d ) ; cout<<" speak" ; speak ( d ) ; cout<<endi;

return 0;

}

运行时的输出结果是【 12 】 。


正确答案:

第2题:

使用VC6打开考生文件夹下的工程test12_3,此工程包含一个test12_3.cpp,其中定义了类Base和类A,类A公有继承 Base,但这两个类的定义都并不完整。请按要求完成下列操作,将程序补充完整。

(1)定义枚举类型变量en,它包含两个枚举符front和back,请在注释“//**1**”之后添加适当的语句。

(2)在类Base中添加常成员虚函数void E()的定义,该函数输出“In Base E!”,请在注释“//**2**”之后添加适当的语句。

(3)在类A中添加常成员虚函数void E()的定义,该函数先调用基类中的虚函数 E()再输出“In AE!”,请在注释“//** 3**”之后添加适当的语句。

(4)完成类A构造函数的定义,请使用参数列表的形式初始化类A的成员,并输出“A constructor.”,请在注释“//** 4**”之后添加适当的语句。

输出结果如下:

Base constructor.

A constructor.

In BaseE!

In AE!

In BaseP!

In A!

1

A destructor.

Base destructor.

注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。

源程序文件test12_3.cpp清单如下:

include<iostream .h>

// ** 1 **

class Base

{

protected:

int b1;

int b2;

public:

Base();

~Base();

int Getb1()const { return b1; }

void Setb1(int x){ b1 = x; }

int Getb2()const { return b2; }

void Setb2(int y) { b2 = y; }

void Print()const {cout<<"In Base P!"<<end1;}

// ** 2 **

};

Base::Base():b1(1),b2(5)

{

cout<<"Base constructor."<<endl;

}

Base::~Base()

{

cout<<"Base destructor."<<endl;

}

class A:public Base

{

protected:

en enA;

public:

A();

~A();

en GetColor()const { return enA; }

void SetColor(en color){ enA = color; }

void InA(){cout<<"In A!"<<endl;}

// ** 3 **

{

Base::E();

cout<<"In AE!"<<endl;

}

};

// ** 4 **

{

cout<<"A constructor."<<endl;

}

A::~A()

{

cout<<"A destructor."<<endl;

}

void main()

{

A a1;

a1.E();

cout<<endl;

a1.Print();

a1.InA();

cout<<a1.Getbl()<<endl;

}


正确答案:(1) enum en{front back}; (2) virtual void E()Const{cout"In BaseE!"endl;} (3) virtual void E()const (4) A::A():enA(front)
(1) enum en{front, back}; (2) virtual void E()Const{cout"In BaseE!"endl;} (3) virtual void E()const (4) A::A():enA(front) 解析:主要考查考生对于枚举和虚函数的定义的掌握,注意(1)中枚举类型使用enum关键字定义,它实际上就是一个有名字的常量,定义格式如下:enum枚举名{枚举表},(2)中常成员函数的定义格式中const的位置不能随便改动,因为const关键字是修饰其右侧文字的,(3)中调用基类函数E的方法Base::E()中使用了作用域符。

第3题:

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

Avoid print( ) const;

Bconst void print( );

Cvoid print( );

Dvoid print(const);


正确答案:A

第4题:

下列程序中声明了两个类AA和BB,其中函数“print”是类AA的成员函数,但是类BB的友元函数。

请在①、②和⑧处各填入正确的内容,使程序能正常运行。

include<iostream.h>

【 】;

class AA{

int t;

public:

AA(int x){t=x;}

void print(BB &b) ;

};

class BB{

int s;

public:

BB(int y){s=y;}

friend void 【 】 print(BB &);

};

void 【 】

{ cout<<"AA:"<<t<<"BB: "<<w.s<<end1;}<

void main(){

AA m(6);

BB n(8);

m.print(n);

}

输出结果为:AA:6;BB:8


正确答案:class BB; AA:: AA::print(BB &w)
class BB; AA:: AA::print(BB &w)

第5题:

以下哪个基类中的成员函数表示纯虚函数( )。

A.virtual void vf (int)

B.void vf(int)=0

C.virtual void vf (=0)

D.virtual void vf (int){}


正确答案:B

第6题:

有如下类声明: class MyBASE { int k: public; void set(int n){k=n;} int get() const{return k;} }; class MyDERIVED: protected MyBASE { protected: int j; public: void set(int m, int n){MyBASE::set(m);j=n;} int get() const{return MyBASE::get()+j;} }; 则类MyDERIVED中保护的数据成员和成员函数的个数是( )。

A.4

B.3

C.2

D.1


正确答案:B

第7题:

有如下类定义: class AA { int a; public: int getRef()const{ return &a; } //① int getValue()const{ return a; } //② void set(int n)const{ a=n; } //③ friend void show(AA aa)const{ cout<<a; } //④ }; 其中的四个函数定义中正确的是( )。

A.①

B.②

C.③

D.④


正确答案:B
解析:此题考查了常成员函数和友元函数的概念。选项A)中,getRef()函数的返回值和函数类型不符,故错误;选项C)中set()常成员函数改变了数据成员的值,这是不允许的;选项D)中show()函数被同时定义成友元函数和常成员函数是错误的。

第8题:

有如下类定义:

class AA

{

int a;

public:

int getRef() const{return &a;} // ①

int getvalue() const{return a;} // ②

void set(int n) const{a=n;} // ③

friend void show(AA aa) const {cout< // ④

};

其中的四个函数定义中正确的是

A . ①

B . ②

C . ③

D . ④


正确答案:B

第9题:

以下基类中的成员函数,表示纯虚函数的是( )。

A.virtual void vf(int);

B.void vf(int)=0;

C.virtual void vf( )=0;

D.virtual void vf(int){ }


参考答案:C

第10题:

有如下类定义:

class Bag {

public:

Bag(int p,char s='M'):price(p),size(s) { count++; }

~Bag() { count--; }

int GetPrice() { return price; }

int GetPrice() const { return price; }

void SetPrice(int val) const { price=val; }

private:

int price;

char size;

static int count;

};

下列关于类成员的叙述中,正确的是( )。

A. 成员函数GetPrice存在重定义

B.构造函数的参数不能设置默认值

C.析构函数中不能访问静态数据成员

D.成员函数SetPrice不能定义为常函数


参考答案:A

更多相关问题