文档库 最新最全的文档下载
当前位置:文档库 › C++期末复习(程序填空题)1

C++期末复习(程序填空题)1

C++期末复习(程序填空题)

1、在下面横线处填上适当字句,完成类中成员函数的定义。
class A{
int * a;
public:
A(int aa=0) {
a=_________;// 用 aa 初始化 a 所指向的动态对象
}
~ A(){_________;}// 释放动态存储空间
};
答:new int(aa) 、delete a

2、下面是一个函数模板,用于计算两个向量的和。在下面横线处填上适当字句,完成函数模板定义。
# include
template
T* f(T* a,T* b,int n)
{
T* c=_________;
for(int i=0;ic [ i ] =_________;
return c;
}
void main()
{
int a [ 5 ] ={1,2,3,4,5},b [ 5 ] ={10,20,30,40},*p;
p=f(a,b,5);
for(int i=0;i<5;i++)
cout<}
答: new int[n]
a [i] +b[i]

3、定义类date的带默认值(为2004年1月1日)的函数与复制构造函数
#include
class date
{ private:
int year,month,day;
public:
date( )
{ year=y; month=m; day=d;}
date( )
{ year=d1.year; month=d1.month ;day=d1.day ; }
};
void main()
{ date d1(2004,10,8);
date d2=d1;
}
答: int y=2004,int m=1,int d=1
const date &d1

4、下面程序的底画线处填上适当的字句,使该程序执行结果为 10 。
#include
class base
{ int X;
public:
void init(int n) {X=n; } //为 X 置值
int Getnum() { return X*X+1; } //取 X 值
};
void main()
{ base test;
test.init(3);
cout<}
5、在下列程序的底画线处填上适当的字句,完成类中成员函数的定义。
#include
class box
{ private ∶
int color, upx,upy, lowx,lowy;
public :
friend int samecolor(line a,box b);
void set _color(int c) {color=c;}
void define _box(int x1,int y1,int x2,int y2)
{upx=x1;upy=y1;}
};
class line
{ private:
int color,startx,starty,len;
public:
friend int samecolor(line a,box b);
void set _color(int c){color=c;}
void define _line(int x,int y){startx=x;}
};
int samecolor(line a,box b)
{ if(a.color= =b.color) return 1; else return 0;}
6、在下面程序的底画线处填上适当的字句,完成程序。
#include
class A
{ public:
void f(int i) { cout<void g() { cout<};
class B :public A
{ public:
void h(){cout<<" h\ n" ;}
A::f;
};
void main()
{ B d1;
d1.f(6);
d1.h();
}

7、下列 shape 类是一个表示形状的抽象类, area() 为求图形面积的函数。从 shape 类派生三角形类 (triangle) 、矩形类 (rectangle)和圆类(circle) ,请给出具体的求面积函数。给出各个类的定义如下所示。
#include
#include
class shape
{ public:
virtual float area()=0 ;
};

class triangle:public shape
{
float a,b,c;
public:
triangle(float x,float y,float z)
{ a=x; b=y; c=z; }
float area()
{ float

t,s;
t=(a+b+c)/2;
s=sqrt(t*(t-a)*(t-b)*(t-c));
return s; }
};
class rectangle:public shape
{
float a,b;
public:
rectangle(int x,int y)
{ a=x; b=y; }
float area()
{ return a*b; }
};
class circle:public shape
{
float r;
public:
circle(float x)
{ r=x; }
float area()
{ return 3.14*r*r; }
};

void main()
{ circle c1(10);
triangle t1(3,4,5);
rectangle r1(10,5);
cout<cout<cout<}

8、写出模板函数实现找出三个数值中按最小值到最大值排序程序。
#include
template
void sort( Type x,Type y, Type z)
{ Type a;
if (x>y) { a=x; x=y; y=a; }
if (x>z) { a=x; x=z; z=a; }
if (y>z) { a=y; y=z; z=a; }
cout <}
void main()
{ sort(2.4,15.7,1.2);
sort(20,15,-82);
sort('A','Y','9');
}

9、完成下面类中的成员函数的定义。
class test
{ private:
int num;
float x;
public:
test(int n ,float f)
{ num=n; x=f; }
test( test &t)
{ num=t.num ; x=t.x; }

};

10、根据下面的主程序,完成类说明的最小形式。
#include
class base
{ private:
int x;
public:
base(int n) { x=n ;}
};
void main()
{ base try(6); }

11、一个类的头文件如下所示,程序,产生对象t ,且 t.num=10 ,并使用 P() 函数输出这个对象的值。
class test
{ private:
int num;
public:
test(int);
void show();
};
test::test(int n){ num=n;}
test::P(){cout<#include
void main()
{ test t(10); t.P( ); }

12、下面是一个类的测试程序,设计出能使用如下测试程序的类:
#include
class Test
{ private:
int x,y;
public:
void initx(int a,int b)
{ x=a; y=b; }
void printx()
{ cout << x<<"-"<};
void main()
{
Test t;
t.initx(300,200);
t.printx();
}
输入结果: 300-200=100

13、下面是日期类的定义,设计出判断闰年和按照“月/日/年”格式显示日期的函数:
class Tdate
{ private:
int month;
int day;
int year;
public:
void Set(int m,int d,int y) //设置日期值
{ month=m; day=d; year=y; }
int IsLeapYear() //判是否闰年
{ }

void Print() //输出日期值
{ }
};
参考答案:
cout <
14、下面是桌子类的定义,设计出带默认值(长为10,宽为8,高为15)的构造函数:
class Desk
{ protected:
int high, width, length;
public:
Desk(); //构造函数声明
Desk( Const Desk &d); //构造函数声明
};

//构造函数定义





//复制构造函数定义:




参考答案:
Desk::Desk(int l=10,int w=8,int h=15)
{ length =l; width=w; high=h; }

Desk( Const Desk &d)
{ length =d. length; width=d. width; high=d. high; }

15、下面是日期类的定义,将它的四个构造函数合并为一个构造函数:
class Tdate
{ protected:
int month, day, year;
public:
Tdate();
Tdate(int d);
Tdate(int d ,int m);
Tdate(int d, int m,int y);
};
Tdate::Tdate()
{ day=15; month=4; year=1995; }
Tdate::Tdate(int d)
{ day=d; month=4; year=1995; }
Tdate::Tdate(int d, int m,)
{ day=d; month=m; year=1995; }
Tdate::Tdate(int d, int m, int y)
{ day=d; month=m; year=y; }

//合并后的构造函数定义:





参考答案:
Tdate::Tdate(int d=15, int m=4, int y=1995)
{ day=d; month=m; year=y; }

16、编写一个类,声明一个数据成员和一个静态数据成员。要求使用构造函数初始化数据成员并把静态数据成员加1,使用析构函数把静态数据成员减1。
class Exam
{ public:
Exam (int v) { x=v; a++; }
~ Exam () { a--; }
static int a;
int get_x() { return x; }
protected:
int x;
};
int Exam::a=0;

17、根据上题编写一个应用程序,创建3个对象,分别显示它们的数据成员与静态数据成员,再析构每个对象,并显示它们对静态数据成员的影响。
#include
void main()
{
Exam * ap=new Exam (5);
cout < get_x () <cout << Exam::a <Exam * bp=new Exam (80);
cout < get_x () <cout << Exam::a <Exam * cp=new Exam (20);
cout < get_x () <cout << Exam::a <delete ap;
cout << Exam::a <delete bp;
cout << Exam::a <delete cp;
cout << Exam::a <}

相关文档