{ m=t.m; n=t.n; }
9、 完成下面类中的成员函数的定义。 class A {
int a,b; public:
A(int aa=0, int bb=0) __【9】___ {} // 分别用aa和bb对应初始化a和b }; main() {
A x(5),y(x); // 定义类A的对象x并用5初始化,同时定义y并用x初始化
___【10】__ ; // 定义p指针,使之指向对象x }
四、 阅读程序,写出各程序运行后的输出结果(每小题5分,共20分)
1、 #include
using namespace std;
void fun(int a,int * b, int *c,int &d); void main( ) {
int a1=10,a2=20,a3=30,a4=40; fun(a1,&a2,&a3,a4);
cout<<”a1=”< void fun(int a,int * b, int *c,int &d) { a++; b++; (*c)++; d++; return; } 2、 #include int i,j,k; for(i=1;i<=4;i++) { for(k=1;k<=4-i;k++) cout<<' '; for(j=1;j<=2*i-1;j++) cout<<'*'; cout< 3、 #include private: int a,b; int getvalue () {return (a>b? a:b);} public: int c void SetValue(int x1,int x2, int x3) { a=x1; b=x2; c=x3; } int GetValue (); }; int CE::GetValue () { int d=getvalue (); return (d>c? d:c); } void main() { int x=5,y=12,z=8; CE ex; ex.SetValue(x,y,z); cout< 4、 #include Point(int x1=0, int y1=0) :x(x1), y(y1) { cout<<\} ~Point() { cout<<\} }; class Circle { Point center; //圆心位置 int radius; //半径 public: Circle(int cx,int cy, int r):center(cx,cy),radius(r) { cout<<\} ~Circle() {cout<<\}; void main() { Circle c(3,4,5); } 五、 程序设计题 (每小题10分,共20分) 1、 设计一程序,它输入10个整数到一个数组中,调整这10个数在数组中的位置,使得其中最小的一个数成为数组的首元素,最大的一个数成为数组的末元素。 2、 定义一个处理日期的类CDate,它有3个私有数据成员:Year,Month,Day和若干个公有成员函数,并实现如下要求:①构造函数重载;②成员函数设置缺省参数;③定义一个成员函数PrintDate来打印日期;④定义一个非静态成员函数SetDate来设置日期。 南昌大学 2009~2010学年第一学期期末考试试卷 C++程序设计(A卷)参考答案 一、 单项选择题(每小题2分,共30分) 1 B2 D3A4C5C6C7A8A9C10C11 B12B 13D 14D15A 二、 判断题(每小题1分,共 10 分) 1×2√ 3×4 ×5√6√7×8√9√10× 三、填空题(每空2分,共20分) 【1】 main (或 主) 【2】 # 【3】 30 【4】 switch 【5】 const 【6】 类名 【7】 两(或 2) 【8】 point& t 【9】 :a(aa),b(bb) 【10】 A * p=&x 四、 程序阅读题 (每小题5分,共20分) 1、 错1个扣1分 a1=10 a2=20 a3=31 a4=41 2、 有三角形状,得3分 * *** ***** ******* 3、 格式不对,扣1分 12 20 4、 顺序不对,扣1分 Point:3 4 Circle radius:5 Circle destructor! Point destructor! 五、 程序设计题 (每小题10分,共20分) 1、 #include int data[SIZE]; int m; cout<<\请输入\个整数:\ for(m=0;m for(int i=1;i else if(data[i]0){ int d=data[0]; data[0]=data[k]; data[k]=d; } if(k int d=data[SIZE-1]; data[SIZE-1]=data[j]; data[j]=d; } cout< for (m=0;m 2、 #include CDate () //函数重载 { Year=1990;Month=1;Day=1; } CDate (int y,int m=1,int d=1) //成员函数设置缺省参数 { Year=y;Month=m;Day=d; } void PrintDate (); //打印日期 void SetDate(int sy,int sm,int sd) //非静态函数设置日期 {Year=sy;Month=sm;Day=sd;} private: int Month,Day,Year; //3个私有数据成员 }; void CDate::PrintDate() { cout<< Year<<\
相关推荐: