资源描述
第11页,共11页
C++程序设计 (A)
一、选择题(每小题2分,共20分)
1、关于对象概念的描述中,( )是错误的。
A. 对象就是C语言中的结构变量
B. 对象代表着正在创建的系统中的一个实体
C. 对象是一个状态和操作(或方法)的封装体
D. 对象之间的信息传递是通过消息进行的
2、对于int *pa[5];的描述,( )是正确的。
A. pa是一个指向数组的指针,所指向的数组是5个int型元素
B. pa是一个指向某个数组中第5个元素的指针,该元素是int型变量
C. pa[5]表示某个数组的第5个元素的值
D. pa是一个具有5个元素的指针数组,每个元素是一个int型指针
3、下列for循环的循环体执行次数为( )。
for(int i=0,j=10;i=j=4;i++,j- -)
A. 0 B. 无限 C. 4 D. 1
4、下述静态数据成员的特征中,( )是错误的。
A. 说明静态数据成员时前边要加修饰符static
B. 静态数据成员要在类体外进行初始化
C. 引用静态数据成员时,要在静态数据成员名前加<类名>和作用域运算符
D. 静态数据成员不是所有对象所共用的
5、( )是析构函数的特征。
A. 一个类中只能定义一个析构函数 B. 析构函数与类名不同
C. 析构函数的定义只能在类体内 D. 析构函数可以有各个或多个参数。
6、已知:p是一个指向类A数据成员m的指针,A1是类A的一个对象, 如果要给m赋值为5,下列表达式( )是正确的。
A. A1.p=5 B. A1->p=5
C. A1.*p=5 D. *A1.p=5
7、关于new运算符的下列描述中,( )是错误的。
A. 它可以用来动态创建对象和对象数组
B. 使用它创建的对象或对象数组可以使用运算符delete删除
C. 使用它创建对象时要调用构造函数
D. 使用它创建对象数组时必须指定初始值
8、派生类的对象对它的基类成员中( )是可以访问的。
A. 公有继承的公有成员
B. 公有继承的私有成员
C. 公有继承的保护成员
D. 私有继承的公有成员
9、下列( )是引用调用。
A.形参是指针,实参是地址值 B.形参和实参都是变量
C.形参是引用,实参是变量 D.形参是变量,实参是引用
10、下述关于开关语句的描述中,( )是正确的。
A.开关语句中default子句可以没有,也可以有一个;
B.开关语句中每个语句序列中必须有break语句;
C.开关语句中default子句只能放在最后;
D.开关语句中case子句后面的表达式可以是整形表达式。
二、填空题(共60分)
(一)、基本概念题(每题2分,共10分)
1、____(1)____类型转换专门针对有虚函数的继承结构,它将基类指针转换成其派生类指针,以做好派生类操作的准备。
2、函数重载是指函数的____(2)_____不同。
3、设有说明语句:
int a[ ][4]={{1,2}, {2,3,4,5}}, *p=(int *)a;
则*(p+5)的值为____(3)_____。
4、设有宏定义和变量说明:
#define T(x,y) x+y
int a= T(3,4) * T(3,5);
则变量a的初值为____(4)______。
5、编程质量的衡量标准包括可读性、易编程性、安全性、可扩充性、效率和____(5)____。
(二)、完善程序题(每空2分,共20分)
1、单链表中结点按元素值递增链接,DeleteAb实现删除结点值在a至b之间的结点(a≤b)的功能。
#include<iostream.h>
struct node
{ int x;
node *link;
}
node *DeleteAb(int a,int b,node *first)
{ node *p=first;
node *q=____(6)______;
while( p && p->x<b )
{ if (____(7)____)
{ q=p; p=p->link; }
else if(q==first)
{ q=___(8)______;
p=first=q;
}
else { q->link=____(9)______;
p=q->link;
}
}
return ____(10)______;
}
void main( )
{ node a[10]={ {1},{2},{3},{4},{5},{6},{7},{8},{9},{10} },*hd=a,*p;
int x,y;
for( int i=0;i<9;i++ )
a[i].link=&a[i+1];
a[9].link=NULL;
p=DeleteAb(x,y,hd);
while(p) { cout<<p->x; p=p->link; }
}
2、程序构造了一个集合类,其中以整形数组存放集合中的元素。通过重载运算符“*”,实现求两个集合交集的运算,即两个集合的共有元素。例如,设集合a,b分别为:
集合a={1,2,3,4,15,6,7,8,9,10};
集合b={1,2,3,4,5,16,17,18,19,20};
求交集a*b的结果为{1,2,3,4}。通过重载运算符“=”,实现两个集合的赋值运算。
#include<iostream.h>
#include“string.h”
class set {
int x[10];
int len;
public:
set( ) { for(int i=0;i<10;i++) x[i]=0; len=0; }
set(int *p, int n)
{ for (int i=0;i<n;i++) x[i]=*p++; len=n; }
friend set operator*(set a,set b);
set & operator=(set b);
int getLen( ) { return len; }
void show(int n) { for(int i=0;i<n;i++) cout<<x[i]<<’\t’; }
};
set operator* (set a,set b)
{ set t; int k=0;
for (int i=0;i<a.len;i++)
for (int j=0;j<b.len;j++)
if (a.x[i]==b.x[j])
{ _____(11)______=a.x[i];
break;
}
_____(12)_____=k;
return t;
}
set & _____(13)______(set b)
{ for (int i=0;i<b.len;i++) x[i]=b.x[i];
len=b.len;
_____(14)______;
}
void main( )
{ int a[10]={ 1,2,3,4,5,6,7,8};
int b[10]={1,12,3,5,8,6,7,14,29};
set set1(a,8),set2(b,9),c;
c=set1*set2;
c.show(____(15)____);
}
(三)、阅读程序题(每题5分,共30分)
1、以下程序的执行结果是___(16)_____ 。
#include <iostream.h>
void main()
{ int x=3,y=3;
switch(x%2)
{ case 1: switch (y)
{ case 0: cout<<"first\t";
case 1: cout<<"second\t"; break;
default: cout<<"hellow\t";
}
case 2: cout<<"third\n";
}
}
2、 以下程序的执行结果是___(17)_____ 。
#include<iostream.h>
int add(int x, int y=8);
void main()
{ int a=5;
cout<<add(a)<<",";
cout<<add(a,add(a))<<",";
cout<<add(a,add(a,add(a)))<<endl;
}
int add(int a,int b)
{ int s=a+b;
return s;
}
3、以下程序的执行结果是___(18)_____ 。
#include<iostream.h>
void f(int &a,int b=3)
{ static int i=2;
a=a*b+i;
i+=a;
}
void main(void)
{ int x=3,y=2;
f(x,y);
cout<<x<<endl;
f(x);
cout<<x<<endl;
}
4、以下程序的执行结果是___(19)_____ 。
#include <iostream>
using namespace std;
class AA
{ int A,B;
public:
AA(int i,int j)
{A=i; B=j; cout<<"Constructor\n";}
AA(AA &obj)
{A=obj.A+10; B=obj.B+20; cout<<"Copy_Constructor\n";}
~AA()
{cout<<"Destructor\n";}
void print()
{cout<<"A="<<A<<",B="<<B<<endl; }
};
void main()
{ AA a1(2,3);
AA a2(a1);
a2.print();
AA *pa=new AA(5,6);
pa->print();
delete pa;
}
5、以下程序的执行结果是____(20)____ 。
#include <iostream>
using namespace std;
class Base{
protected:
int x;
public:
Base(int a){x=a;}
};
class A:public Base{
public:
A(int a):Base(a){ }
int GetX() {return x;}
};
class B:public Base{
public:
B(int a):Base(a){ }
int GetX() {return x;}
};
class C:public B,public A {
public:
C(int a):A(a+10),B(a+20){ }
};
void main(){
C c(10);
cout<<c.A::GetX()<<','<<c.B::GetX()<<endl;
}
6、以下程序的执行结果是___(21)_____ 。
#include<iostream>
using namespace std;
class A{
int x,n;
int mul;
public:
A(int a,int b) { x=a;n=b;mul=1; }
virtual int power( )
{ mul=1;
for(int i=1;i<=n;i++) mul*=x;
return mul;
}
void show(void)
{ cout<<mul<<'\t'; }
};
class B:public A{
int y,m;
int p;
public:
B(int i,int j,int k,int h):A(i,j) { y=k;m=h;p=10; }
int power( )
{ p=1;
for(int i=1;i<=m;i++) p*=y;
return p;
}
void show(void)
{ A::show(); cout<<p<<'\n'; }
};
void fun(A *f)
{ cout<<f->power()<<'\n'; }
void main()
{ A a(5,3); B b(2,4,3,3);
fun(&a); fun(&b); b.show();
}
三、编程题(每题10分,共20分)
12 35
77 91
123 789
24 28
64 112
1、有一个文件abc.in,其中含有一些整数对,求出这些整数对的最大公约数,并对这些最大公约数从小到大排序输出。要求使用向量存放数据,程序分为输入、排序、输出三个函数完成,其样板文件内容如下:
abc.in
2、定义一个处理日期的类TDate,它有3个私有数据成员:Month,Day,Year和若干个公有成员函数,并实现如下要求:①构造函数重载;②成员函数设置缺省参数;③定义一个友元函数来打印日期;④定义一个非静态成员函数设置日期;⑤可使用不同的构造函数来创建不同的对象。
一、 单选题(每题1分,共15分)
C C B D D C A A D C B A B C D
二、 基本概念填空题(每空1分,共15分)
1、传地址 引用
2、-2
3、0
4、inline
5、0 2
6、26 14
7、整型 字符型
8、ABC
9、2
10、16
11、3
三、阅读程序题(每题4分,共20分)
1、a
2、2 5
3、3
4、12 28
5、2 4 7 11 16
四、完善程序题(每空2分,共20分)
1、(1)int y[ ],int n
(2)y[i]
(3)y[j]=0
(4)count%10==0
2、(5)‘\0’
(6)n++
(7)‘\0’
3、(8)p=0
(9)p=i
(10)sizeof(int)
五、改错题(10分)
//11 while (p->Next!=NULLl)
//14 s=p;
//15 while (q)
//17 if(q->Data==p->Data)
//19 s->Next=q->Next;
六、程序设计题(20分)
1、(10分)
#include<iostream.h>
struct score{
char name[20];
long num;
double cpp;
double english;
double math;
double ave;
};
void compute(score x[],int n)
{
for(int i=0;i<n;i++)
x[i].ave=(x[i].cpp+x[i].english+x[i].math)/3;
}
void input(score x[],int n)
{
for(int i=0;i<n;i++){
cout<<"输入第"<<i+1<<"个学生的姓名: ";
cin.ignore(1);
cin.getline(x[i].name,20);
cout<<"输入第"<<i+1<<"个学生的学号: ";
cin>>x[i].num;
cout<<"输入第"<<i+1<<"个学生的C++成绩: ";
cin>>x[i].cpp;
cout<<"输入第"<<i+1<<"个学生的英语成绩: ";
cin>>x[i].english;
cout<<"输入第"<<i+1<<"个学生的数学成绩: ";
cin>>x[i].math;
}
}
void output(score x[],int n)
{
for(int i=0;i<n;i++){
cout<<"第"<<i+1<<"个学生的姓名: "<<x[i].name<<endl;
cout<<"第"<<i+1<<"个学生的学号: "<<x[i].num<<endl;
cout<<"第"<<i+1<<"个学生的C++成绩: "<<x[i].cpp<<endl;
cout<<"第"<<i+1<<"个学生的英语成绩: "<<x[i].english<<endl;
cout<<"第"<<i+1<<"个学生的数学成绩: "<<x[i].math<<endl;
cout<<"第"<<i+1<<"个学生的平均成绩: "<<x[i].ave<<endl;
}
}
void main()
{
int n;
cout<<"请输入班级人数";
cin>>n;
score *p=new score[n];
input(p,n);
compute(p,n);
output(p,n);
delete[]p;
}
2、(10分)
#include<iostream.h>
void sort(int x[],int n)
{
int t,i,j;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(x[i]>x[j]){
t=x[i];x[i]=x[j];x[j]=t;
}
}
void input(int x[],int n)
{
for(int i=0;i<n;i++){
cout<<"输入第"<<i+1<<"个整数: ";
cin>>x[i];
}
}
void output(int x[],int n)
{
for(int i=0;i<n;i++)
cout<<x[i]<<'\n';
}
void main()
{
int y[10];
input(y,10);
sort(y,10);
output(y,10);
}
展开阅读全文