C++试题及答案 联系客服

发布时间 : 星期三 文章C++试题及答案更新完毕开始阅读6a8dc5b3866fb84ae55c8d99

4. 输出最小值,有一处错误。 #include class Test {int a,b; int getmin()

{return (a

void setValue(int x1,int x2,int x3) {a=x1;b=x2;c=x3;} int GetMin(); };

int Test::GetMin() {int d=getmin(); return (d=d

void main() {Test t1;

t1.setValue(34,6,2);

cout<

答案:cout< #include using namespace std; template void Swap(T& a,T& b) {T temp;

temp=a,a=b,b=temp; }

void main() {int a=5,b=9;

char s1[]=\[]=\Swap(a,b); Swap(s1,s2);

cout<<\cout<<\}

答案:char s1[]=\[]=\使用Swap(s1,s2)调用交换的是地址。字符指针作实 参,形参值发生改变,实参也就发生变化。 [修改]char *s1=\

四、完成程序题(本大题共5小题,每小题4分,共20分) 1. 在下划线处填上缺少的部分。 #include class A {int a,b; public:

_____;//定义构造函数,使参数i和j的默认值为0 {a=i;b=j;}//在函数体中用i初始化a,用j初始化b }; main() {A *p;

_____;//调用带参构造函数生成由p指向的动态对象 //使a和b成员分别被初始化为4和5 }

答案:A(int i=0,int j=0),p=new A(4,5)

[解析]构造函数带默认参数为0,使用new运算符动态分配对象空间,同时初始对象成员值 4,5。

2. 在下面程序横线处填上适当内容,使程序执行结果为: S=2 S=5 S=9

#include void sum(int i) {static int s;

_________;

cout<<\}

void main (void) {int i;

for (i=0;________) sum(i); }

答案:s=s+i+2;,i<3,i++

[解析]根据结果和调用形式,得出规律。注意静态成员能保留上次运行的结果。循环了3次 ,退出循环的条件。

3. 下面程序运行的结果是:5+10=15。 #include class Test { private: int x,y; public:

Test() {x=y=0;}

void Setxy(int x,int y) {______} void show(){______} };

void main() {Test ptr;

ptr.Setxy(5,10); ptr.show(); }

答案:(*this).x=x; (*this).y=y;,cout< #include class Arr {protected: float *p;

int n;//数组大小(元素个数) public:

Arr(int sz=10) { n=sz;

p=new float[n]; }

~Arr(void) {

_________ }

int Getn(void) const {

return n; }

float & operator[](int i) {

________ }

void Print(); };

void Arr::Print() {int i;

for(i=0;i< this->Getn();i++) {if (i==0) cout << endl;

cout<

cout<

void main() {Arr a(20);

for (int i=0;i

a.Print(); }

答案:delete p;,return p[i];

[解析]在析构函数中释放对象空间。第二个是对[]运算符的重载,函数返回类型是实型,形 参i,取得下标为i的元素的值。

5. 请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为: 11,10 13,12

#include class A {int a; public:

A(int i=0){a=i;}

int Geta(){return a;}

void show(){cout<

class B {A a; int b; public:

B(int i,int j)_________ {}

void show(){cout<

void main()

{B b[2]={B(10,11),B(12,13)}; for(int i=0;i<2;i++) __________ }

答案::a(j),b(i),b[i].show();

[解析]在构造函数中对数据成员初始化,从结果先输出a,后b,所以对a=j,b=i;在循环中 输出成员,调用show成员。

五、程序分析题(本大题共4小题,每小题5分,共20分) 1. 给出下面程序输出结果。 #include class a {public:

a(int i=10){x=i;cout<<\int x; };

class b:public a {public:

b(int i):A(i){x=i;cout<<\private: a A; int x; };

void main() {b B(5); }

答案:a:10 a:5 b:5,10

[解析]定义对象B,先调用基类构造函数,在b构造函数中使用的是A(i),注意大小写,不 是a(i),也就是说调用基类的构造函数时没有实参值,所以采用默认值;在初始化类成员A,即 A(i),i=5,所以输入为a:5;最后是b类的构造函数,x=5,来自基类x=10,输出b:5,10。 2. 运行程序,写出程序执行的结果。 #include class Location {public: int X,Y;

void init(int initX,int initY); int GetX(); int GetY(); };

void Location::init (int initX,int initY) {X=initX;

Y=initY; }

int Location::GetX() {return X; }

int Location::GetY() {return Y; }

void display(Location& rL)

{cout<

void main()

{Location A[5]={{5,5},{3,3},{1,1},{2,2},{4,4}}; Location *rA=A; A[3].init(7,3); rA->init(7,8);

for (int i=0;i<5;i++) display(*(rA++)); }

答案:7 8 3 3 1 1 7 3 4 4

[解析]对象数组的使用。使用数组对象修改了A[3]元素的值,又使用指针修改指针所指向的 第一个元素的值,因此修改了A[0]和A[3]元素的值。 3. 给出下面程序输出结果。 #include

int a[8]={1,2,3,4,5,6,7}; void fun(int *pa,int n); void main() {int m=8; fun(a,m);

cout<

void fun(int *pa,int n) {for (int i=0;i

答案:28

[解析]数组名与指针都表示地址,只是数组名是常地址,不能改变;指针是地址变量,使用时 可以当数组名使用。

4. 给出下面程序输出结果。 #include class A {int *a; public:

A(int x=0):a(new int(x)){} ~A() {delete a;}

int getA() {return *a;} void setA(int x) {*a=x;} };

void main() {A x1,x2(3); A *p=&x2;

(*p).setA(x2.getA()+5); x1.setA(10+x1.getA());

cout<

答案:108

[解析]p指向对象x2,x2.getA()+5该值为8 即x2.a=8;10+x1.getA()为10,x1.a=10。 六、程序设计题(本大题共1小题,共10分) 1. 已知交通工具类定义如下。

要求:(1)实现这个类;(2)定义并实现一个小车类car,是它的公有派生类,小车本身的私有 属性有载人数,小车的函数有init(设置车轮数,重量和载人数),getpassenger(获取载人数 ),print(打印车轮数,重量和载人数)。 class vehicle