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

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

class Test {private: int x,y=20; public:

Test(int i,int j){x=i,y=j;} int getx(){return x;} int gety(){return y;} };

void main()

{Test mt(10,20);

cout<

答案:int x,y=20;在类内部不能对数据成员直接赋值。 [修改]int x,y;

2. #include class Test {int x,y; public:

fun(int i,int j) {x=i;y=j;} show()

{cout<<\if(y)

cout<<\cout<

void main() {Test a; a.fun(1); a.show(); a.fun(2,4); a.show(); }

答案:int i,int j调用时,既有一个参数,也有两个参数,且没有重载,所以参数需要带默认 值。所以int i,int j错误。

[修改]int i,int j=0//注j只要有一个int类型的数据就行。 3. #include class A {int i; public:

virtual void fun()=0; A(int a) {i=a;} };

class B:public A {int j; public: void fun()

{cout<<\\n\

B(int m,int n=0):A(m),j(n){} };

void main() {A *pa; B b(7); pa=&b; }

答案:B(int m,int n=0):A(m),j(n){}因为基类是抽象类,不能被实例化,所以在派生类中不能 调用初始化基类对象。所以B(int m,int n=0):A(m),j(n){}错误,删去A(m)。 [修改]B(int m,int n=0):j(n){} 4. #include class X {public: int x; public: X(int x)

{cout<x=x<

X(X&t) {x=t.x;

cout<

void fun(X); };

void fun(X t)

{cout<

答案:cout<x=x<x=x表达式的值要加括号。 [修改]cout<<(this->x=x)< #include class Bas {public:

Bas(char *s=\\0\void show(); protected:

char name[20]; }; Bas b;

void show()

{cout<<\void main()

{Bas d2(\show(); }

答案:void show();是普通函数不是成员函数,但是要访问类成员,需要定义为友元函数。 [修改]friend void show();

四、完成程序题(本大题共5小题,每小题4分,共20分)

1. 在下面程序横线处填上适当字句,以使该程序执行结果为: 50 4 34 21 10

0 7.1 8.1 9.1 10.1 11.1 #include template void f (__________) {__________;

for (int i=0;i

t=a[i], a[i]=a[n-1-i], a[n-1-i]=t; }

void main ()

{int a[5]={10,21,34,4,50};

double d[6]={11.1,10.1,9.1,8.1,7.1}; f(a,5);f(d,6);

for (int i=0;i<5;i++) cout <

for (i=0;i<6;i++) cout << d[i] << \cout << endl; }

答案:T a[],int n,T t=0;

[解析]不同的数据类型的调用,使用了模板。f函数增加t变量,因为实参类型不同,所以t的 类型应该是T类型的。

2. 在下面程序的底画线处填上适当的字句,使该程序执行结果为40。 #include class Test { public: ______;

Test (int i=0) {x=i+x;} int Getnum()

{return Test::x+7;} };

_______; void main()

{Test test;

cout<

答案:static int x;,int Test::x=30;

[解析]从成员函数访问方式类名::成员可知是静态成员所以static int x;从结果要对初始 化为30,且在类外进行初始化, int Test::x=30;。

3. 在下列程序的空格处填上适当的字句,使输出为:0,2,10。 #include #include class Magic {double x; public:

Magic(double d=0.00):x(fabs(d)) {}

Magic operator+(______) {

return Magic(sqrt(x*x+c.x*c.x)); }

_______operator<<(ostream & stream,Magic & c) { stream<

void main() {Magic ma;

cout<

答案:operator+(Magic&c),friend ostream&operator

[解析]对加法进行重载,operator+(Magic & c),是对插入符进行重载,要访问成员所以定义 为友元函数,friend ostream & operator。

4. 下面是一个输入半径,输出其面积和周长的C++程序,在下划线处填上正确的语句。 #include _________; _________; void main() {double rad; cout<<\cin>>rad;

double l=2.0*pi*rad; double s=pi*rad*rad;

cout<<\\n The long is:\cout<<\:\

答案:using namespace std,#define pi 3.14159

[解析]进行输入或输出要引入iostream, 所以using namespace std;从标点看没有分号,所以 使用宏定义,#define pi 3.14159。 5. 程序实现大写字母转换成小写字母。 #include void main() {char a; _______; cin>>a;

if(_______) a=a+i;

cout<

答案:int i=32;,a>=A && a<=Z

[解析]大写字母变小写字母相差32,需要对i声明并初始化。大写字母变小写字母。要判断字 符是大写字母。

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

virtual void print()

{cout<< \};

class b:public a {};

class c:public b {public:

void print(){cout<<\};

void show(a *p) {(*p).print(); }

void main() {a a; b b; c c;

show(&a); show(&b); show(&c); }

答案:a prog... a prog... c prog...

[解析]考查多态性的。a类对象调用本身的虚函数,b类因为没有覆写print,所以仍然调用基 类的虚函数。而c类重新定义print虚函数,所以调用c类的print。 2. 给出下面程序输出结果。 #include #include #include bool fun(long n); void main()

{long a=10,b=30,l=0; if(a%2==0) a++;

for(long m=a;m<=b;m+=2) if(fun(m)) {if(l++==0) cout <

cout <

bool fun(long n)

{int sqrtm=(int)sqrt(n); for(int i=2;i<=sqrtm;i++) if(n%i==0) return false; return true; }

答案:11 13 17 19 23 29

[解析]循环体用来判断n是否是质数的函数,在main函数判断10~30之间质数。 3. 给出下面程序输出结果。 #include class Test {int x,y; public:

Test(int i,int j=0) {x=i;y=j;}

int get(int i,int j) {return i+j;} };

void main()

{Test t1(2),t2(4,6);

int (Test::*p)(int,int=10); p=Test::get;

cout<<(t1.*p)(5)<

cout<<(p1->*p)(7,20)<

答案:15 27

[解析]指向类成员函数的指针的使用,*p指向Test类中有两个参数的函数的一个指针。 P=Test::get.这样p就和get发生了联系。(t1.*p)(5)等价于调用一个参数的get函数。