计算机二级

有以下类定义 class Point{ 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<<')' << end1;} private: int_x,_y; }; 下列语句中会发生编译错误的是A.Point pt;pt.Print();B.const Point p

题目

有以下类定义 class Point{ 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<<')' << end1;} private: int_x,_y; }; 下列语句中会发生编译错误的是

A.Point pt;pt.Print();

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

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

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

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

第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题:

分析以下程序的执行结果【】。includeclass Sample{int x, y;public:Sample() {x=y=0;

分析以下程序的执行结果【 】。

include<iostream.h>

class Sample

{

int x, y;

public:

Sample() {x=y=0; }

Sample(int a, int b) { x=a; y=b;}

~Sample()

{

if(x==y)

cout<<"x=y"<<end1;

else

cout<<"x!=y" <<end1;

}

void disp()

{

cout<<"x="<<x<<",y="<<y<<end1;

}

};

void main()

{

Sample s 1 (2,3);

s1.disp();

}


正确答案:x=2y=3 x!=y
x=2,y=3 x!=y

第3题:

有如下类定义:

class Point

{

int x_, y_;

public:

Point():x_(0), y_(0){}

Point(int x, int y = 0):x_(x),y_(y){}

};

若执行语句

Point a(2), b[3] , *c[4];

则 Point 类的构造函数被调用的次数是

A . 2 次

B . 3 次

C . 4 次

D . 5 次


正确答案:C

第4题:

有以下程序:includeusingnamespacestd;definePI3.14classPoint{private: intx,y;pub

有以下程序: #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; } int gety() { return y; } }; class Circle : public Point { private: int r; public: Circle(int a,int b,int c):Point(a,b) { r=c; } int getr() { return r; } double area() { return PI*r*r; } }; int main() { Circle c1(5,7,10); cout<<cl.area()<<endl; return 0; } 程序执行后的输出结果是

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。

第5题:

有以下程序: include using namespace std; class Point' { public: void SetPoint(

有以下程序: #include <iostream> using namespace std; class Point' { public: void SetPoint(int x,int y); void Move(int xOff,int yOff); int GetX() { return X; } int GetY() { return Y; } private: int X,Y; }; void Point::SetPoint(int x, int y) { X=x; Y=y; } void Point: :Move(int xOff, int yOff) X+=xOff; Y+=yOff; } int main () { Point p1; p1.SetPoint(1,2); p1.Move (5, 6); cout<<"Point1 is ("<<p1.GetX()<<','<<p1.GetY()<<")"<<end1; return 0; } 执行后的输出结果是( )。

A.Point1 is (6,8)

B.Point1 is (1,2)

C.Point1 is (5,6)

D.Point1 is (4,4)


正确答案:A
解析:本题考核对象的定义与使用。程序中定义了一个类Point,在主函数中定义了一个Point类的对象p1,然后通过对象p1调用其成员函数SetPoint()和Move()实现移位的操作。

第6题:

有如下类定义:

class Point{

public:

Point(int xx=0,int yy=0):x(xx),y(yy) { }

private:

int x,y;

};

class Circle:public Point{

public:

Circle(int r):radius(r) { }

private:

int radius;

};

派生类Circle中数据成员的个数是( )。

A、3

B、1

C、5

D、2


答案:A
解析:本题考查默认构造函数和带参数的构造函数,题目中定义一个对象a(2)以及对象数组b[3],共执行3次构造函数,对象指针不调用构造函数。


第7题:

如下的类定义,括号里应填( )。 class Myclass { public: MyClass(int a =0,int b =0) { X=a; Y=b; void Change ( ) const { X- =10; Y+ =10; public: ( )int X,Y;

A.static

B.const

C.mutable

D.可以不添内容


正确答案:C
解析:常成员函数一般不能修改对象的数据成员的值,如果一定要修改,必须得在被修改的数据成员定义前加上关键字mutable。

第8题:

下列函数原型声明中错误的是

A.void Fun(int x=O,int y=0);

B.void Fun(int x,int y);

C.void Fun(int x,int y=0);

D.void Fun(int x=0,int y);


正确答案:D
解析:本题考核函数的定义方法和调用方法。说明一个函数的格式为:函数类型>函数名>,(下函数参数表>);在C++中,允许在函数的说明或定义时给一个或多个参数指定默认值。但一旦为某个给定参数定义了缺省值,必须为后继的所有参数也定义缺省值。由此可知,选项D是错误的。

第9题:

下面是类MyClass的定义,对定义中各种语句描述正确的是( )。

class MyClass { publiC: void MyClass(int a) //①

{ x=a; ) int func(int a,int b) //②

{ x=a; y=b; } int func(int a,int b,int c=0) //③

{ x=a; y=b; z=c; } static void resetValue() //④

{ x=0; } private: int X,y,Z; };

A.语句①是类MyClass的构造函数定义

B.语句②和语句③实现类成员函数的重载

C.语句④实现对类成员变量x的清零操作

D.语句①、②、③和④都不正确


正确答案:D
解析:类的构造函数没有返回类型,所以语句①是错误的;语句②和语句③将会使函数func()的调用产生歧义;成员函数resetValue()是静态成员函数,那么它不能访问类MyClass中的非静态数据成员,所以语句④也不正确。

第10题:

下列程序的输出结果是includeclass Myclass{public : Myclass( int i=0,int j=0){x

下列程序的输出结果是 #include<iostream.h> class Myclass{ public : Myclass( int i=0,int j=0) {x=i; y=j; } void show( ){cout<<"x="<<x<<" "<"y="<<y<<endl;} void show( )const{cout<<"x="<

A.x=4,y=3;x=7,y=8

B.x=3,y=4;x=7,y=8

C.x=7,y=8;x=4,y=3

D.x=8,y=7;x=7,y=8


正确答案:A
解析:在Myclass类中定义了两个同名函数show,其中一个是常成员函数。在main函数中定义了两个对象myl,my2,其中对象my2是常对象。这两个对象调用成员函数show时,通过对象myl调用的是没有用const修饰的一般成员函数,而通过对象my2调用的是const修饰的常成员函数。

更多相关问题