最新南航C++(A)试卷
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1.在下列成对的表达式中,运算结果类型相同的一对是( )
A.7/2和7.0/2.0 B.7/2.0和7/2
C.7.0/2和7/2 D.7.0/2.0和7.0/2
2.能正确表示“当x的取值在[1, 10]和[200, 210]范围内为真,否则为假”的表达式是()A.(x>=1)&&(x<=10)&&(x>=200)&&(x<=210)
B.(x>=1)||(x<=10)||(x>=200)||(x<=210)
C.(x>=1)&&(x<=10)||(x>=200)&&(x<=210)
D.(x>=1)||(x<=10)&&(x>=200)||(x<=210)
3.使用值传递方式将实参传给形参,下列说法正确的是()
A.引用类型形参不分配内存空间,仅仅是实参的别名
B.引用类型形参分配内存空间,形参与实参占用不同的内存空间
C.指针类型形参分配内存空间,形参接受实参传递的数值
D.指针类型形参不分配内存空间,仅仅是实参的别名
4.若有宏定义:#define MOD(x, y) x%y 则执行以下语句后输出为()int z, a = 15, b = 100;
z = MOD(b, a);
cout<< z + +;
A.11 B.10 C.6 D.宏定义不合法5.若有以下定义和语句,则对数组元素a[1][2]的正确引用为()。
int a[2][3], (*p)[3]; p=a;
A.(*(p+1))[2] B.*p[1][2] C.*(p+1)[2] D.*(p+1)+2
6.内联函数的特点是( )
A.减少代码量,加快访问速度 B.减少代码量,减缓访问速度
C.增加代码量,减缓访问速度D.增加代码量,加快访问速度
7.C++类中定义的成员默认访问属性为( )
A.public B.private C.protected D.friend
8.类的私有成员可在何处被访问( )
A.本类的成员函数中B.本类及派生类的成员函数中
C.通过对象名在任何位置D.不可访问
9.对类A,拷贝构造函数的原型是( )
A.A::A(const A&);B.A::A(const A *);
C.A::A(const A);D.A::A( );
10.下列关于析构函数描述正确的是( )
A.可以重载B.函数体中必须有delete语句
C.返回类型必须是void类型D.不能指定返回类型
11.如果类A被声明成类B的友元,正确的是( )
A.类A的成员函数即为类B的成员函数
B.类B的成员函数即为类A的成员函数
C.类A的成员函数不可以访问类B的成员
D.类B不一定是类A的友元
12.this指针存在的目的是()
A.保证基类公有成员在派生类中可以被访问
B.保证每个对象拥有自己的数据成员,但共享处理这些数据成员的代码
C.保证基类保护成员在派生类中可以被访问
D.保证基类私有成员在派生类中可以被访问
13.在公有继承的情况下,允许派生类直接访问的基类成员包括()A.只能是公有成员B.公有成员和保护成员
C.公有成员、保护成员和私有成员D.只能是保护成员
14.下列关于虚函数的描述中,正确的是()
A.虚函数是一个static类型的成员函数
B.虚函数是一个非成员函数
C.基类中采用virtual说明一个虚函数后,派生类中相同原型的函数也是虚函数D.派生类中的虚函数必须与基类中相同函数名的虚函数具有不同的参数个数或类型15.下列关于运算符重载的描述中,正确的是()
A.运算符重载为成员函数时,若参数表中无参数,重载的是一元运算符
B.一元运算符只能作为成员函数重载
C.二元运算符重载为非成员函数时,参数表中有一个参数
D.C++中可以重载所有的运算符
1. #include <iostream.h>
void main( )
{
int x=4 ;
if ( x++ > 5) cout<< x ;
else cout<< x--;
} 程序运行结果为:。
2.#include <iostream.h>
void fun(int &x,int y,int *z)
{
int t=y; x=*z; *z=t;
}
void main( )
{
int a[3]={23,42,56};
fun(a[0],a[1],&a[2]);
cout << a[0] << ", " << a[1]<< ", " << a[2]<<endl;
} 程序运行结果为:。
3. #include <iostream.h>
int dtoh (int c[],int n)
{
int i=0;
while(n)
{ c[i++]=n%16; n/=16; }
return(i);
}
void main()
{
int c[20], n=698, i;
i=dtoh (c,n);
for(i--;i>=0 ; i--)
if (0<=c[i]&&c[i]<=9) cout <<char (c[i]+'0');
else cout<<char(c[i]-10+'A');
} 程序运行结果为:。
4.#include<iostream.h>
#include<string.h>
class Student
{ char Num[10];
char *Name;
int Score;
public:
Student(char *nump, char *namep, int score)
{ if(nump) strcpy(Num, nump);
else strcpy(Num, "");
if(namep)
{ Name=new char[strlen(namep)+1];
strcpy(Name, namep);
}
else Name=0;
Score=score;
cout<<"Constructor Called!\n";
}
~Student( )
{ if(Name) delete [ ] Name;
cout<<"Desturctor Called!\n";
}
void Show( )
{ cout << Num <<'\t'<< Name << '\t' << Score << endl; } };
void main( )
{ Student a("0409204", "George", 90);
a.Show( );
} 程序运行结果为:。
5. #include <iostream.h>
class A
{ int num1;
static int num2;
public:
A( ) { num1=1; num2--; cout<<num1<<'\t'<<num2<<endl; }
static int getnum(A &n1){ return n1.num1+num2; } };
int A::num2=5;
void main()
{ A n1,n2;
cout<<A::getnum(n1)<<endl;
} 程序运行结果为:。
6.#include <iostream.h>
class A
{
protected: int x;
public: A(int a=0){ x=a; }
void Show( ) { cout << "A:x=" << x << '\n' ; } };
class B : public A
{
protected: int x;
public: B(int a,int b) :A(a) {x=b;}
void Show( ) {cout<<"B:x="<<x<<endl; } };
void main(void)
{
B b(2,8);
b.A::Show( ); b.Show( );
} 程序运行结果为:。
7. #include <iostream.h>
class Sample
{ int x, y;
public:
Sample( int a=0, int b=0)
{ x=a; y=b;
cout<<x<<','<<y<<" default constructor\n";
}
Sample( Sample &s )
{ x=s.x; y=s.y;
cout<<x<<','<<y<<" copy constructor\n";
}
~Sample( )
{ cout<<x<<','<<y<<" destructor\n"; }
void add( )
{ x+=10; y+=10; }
};
void main( )
{ Sample a (1, 2);
Sample b=a;
Sample *ptr=new Sample(2,3);
ptr->add( );
delete ptr;
} 程序运行结果为:。
8. #include <iostream.h>
class A
{ int x;
public:
A() { x=10; }
virtual void print() { cout<<”x=”<<x<<’\t’;}
};
class B:public A
{ int y;
public: B() { y=20; }
void print() { cout<<”y=”<<y<<’\t’; }
};
class C:public A
{ int z;
public: C() { z=30; }
void print() { cout<<”z=”<<z<<’\n’; }
};
void main()
{ A a,*pa; B b; C c;
pa=&a; pa->print();
pa=&b; pa->print();
pa=&c; pa->print();
} 程序运行结果为:。
三.完善程序(每空2分,共16分)
1.以下程序是用选择法对对数组a中的元素按由小到大顺序排序。
请填空:
#include <iostream.h>
void sort(int a[],int n)
{ int i,j,p, t;
for(i=0;i<n-1;i++)
{ (1) ;
for( j=i+1; j<n; j++)
if( (2) ) p=j;
if (p!=i) {t=a[i];a[i]=a[p];a[p]=t;}
}
}
void main()
{ int a[10]={2,6,9,4,1,7,14,18,56,5}, i,n=10;
(3) ;
for(i=0;i<n;i++) cout<<a[i]<<" ";
}
2. 请完善程序,实现链表操作:
#include <iostream.h>
class Node
{ int data;
(4);
public: friend class List;
};
class List
{ Node *head;
public:
List(int d)
{ head = new Node; head->data = d; head->next = NULL; } List( ) { head = NULL; }
void print( ) //输出链表
{ Node *p;
p=head;
while(p!=NULL)
{ cout<<p->data<<'\t';
(5);
}
}
~List()
{ while(head)
{ Node *p=head;
head = head->next;
delete p;
}
}
};
void main()
{ List a(100); a.print(); }
3.将数值1~100及其平方根写入文件sqrttable.txt
#include <iostream.h>
(6)
#include <math.h>
void main( )
{ double x;
ofstream out;
(7);
for(x=1; x<=100; x++)
out<<x<<'\t'<<sqrt(x)<<endl;
(8);
}
四.编程题(每题10分,共30分)
1.编写一个函数void my_strcpy(char s1[],char s2[]),
将数组s2中的字符串复制到数组s1中。
要求:(1)必须自己编写函数实现字符串复制,不可调用系统strcpy函数。
(2)在主函数中输入两个字符串,调用my_strcpy函数实现字符串复制,并在主函数中将复制后的字符串s1的内容输出。
2.定义平面点Point类,用友元函数分别实现点类的前置++和后置++运算符的重载。
要求:
(1)定义平面点类(Point):
私有数据成员:int x,y;
构造函数:完成点的初始化
友元函数:实现前置++运算符的重载
友元函数:实现后置++运算符的重载
函数:在屏幕上显示点的坐标
(2)在主函数中建立4个Point对象p1,p2,p3,p4。
p1的默认值为(2,3),在生成p2对象的时候,使用p1初始化p2对象。
分别使用重载运算符计算++p1和p2++的值,将计算结
果先后赋值给p3和p4对象,最后分别输出p1,p3和p2,p4的值。
3.定义直角坐标系上的一个点Point类,并作为基类,派生出描述一个直线Line类(两点坐标确定一条直线),再派生出一个矩形Rectangle类(左上角和右下角坐标确定一个矩形)。
在主函数中计算并且输出直线的长度和矩形的面积。
各类成员的具体要求如下:
点类(Point)
(1)保护数据成员:int x,y;
(2)构造函数:完成点的初始化。
线类(Line):公有继承点类
(1)保护数据成员:int x1,y1;第二个点的坐标
(2)构造函数:完成点的初始化。
(3)函数:float LineLen(); 求线的长度
矩形类(Rectangle):公有继承点类
(1)保护数据成员:int x1,y1;右下角的坐标
(2)各类构造函数:完成点的初始化。
(3)函数:float Area(); 求矩形的面积
一、选择题(每题2分,共30分)
1-5 DCABA 6-10 DBAAD 11-15 DBBCA
二、试写出下列程序的输出结果(每题3分,共24分)
1)5
2)56,42,42
3)2BA
4)Constructor Called!
0409204 George 90
Desturctor Called!
5)1 4 6)A:x=2
1 3 B:x=8
4
7)1,2 default constructor
1,2 copy constructor
2,3 default constructor
12,13 destructor
1,2 destructor
1,2 destructor
8)x=10 y=20 z=30
三、填空题(每题2分,共16分)
(1) p=i
(2) a[j]<a[p]
(3) sort(a,n) 或sort(a,10)
(4) Node *next
(5) p = p->next
(6) #include <fstream.h>
(7) out.open("sqrttable.txt ")
(8)out.close()
四、编程题(每题10分,共30分)
1. #include <iostream.h>
void my_strcpy(char s1[],char s2[])
{ int i;
for(i=0;s2[i]!='\0';i++)
s1[i]=s2[i];
s1[i]='\0';
}
void main()
{ char a[20],b[10];
cin>>a>>b;
my_strcpy(a,b);
cout<<"a="<<a<<endl;
}
2.
#include <iostream.h>
class Point
{ int x,y;
public:
Point(int a=0,int b=0) { x = a; y = b; }
friend Point operator ++(Point &m) { return Point(++m.x,++m.y); } friend Point operator ++(Point &m,int)
{ Point t=m;
m.x++;
m.y++;
return t;
}
void Show() { cout<<"x="<<x<<"y="<<y<<endl; }
};
void main()
{
Point p1(2,3),p3,p4;
Point p2=p1;
p3 = ++p1; p1.Show(); p3.Show();
p4 = p2++; p2.Show(); p4.Show();
}
3.
#include <iostream.h>
#include <math.h>
class Point
{
protected:
int x,y;
public:
Point(int a=0,int b=0) { x = a; y = b; } };
class Line: public Point
{
protected:
int x1,y1;
public:
Line(int a=0,int b=0,int c=0,int d=0):Point(a,b)
{ x1 = c; y1 = d; }
float LineLen()
{ return sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)); }
};
class Rectangle: public Point
{
protected:
int x1,y1;
public:
Rectangle(int a=0,int b=0,int c=0,int d=0):Point(a,b)
{ x1 = c; y1 = d; }
float Area() { return float(abs(x-x1)*abs(y-y1)); } };
void main()
{ Line a(0,0,1,1);
Rectangle b(2,2,4,4);
cout<<"length="<<a.LineLen()<<endl;
cout<<"area="<<b.Area()<<endl;
}。