C++面向对象程序设计上机考试题库.pdf
文本预览下载声明
C++面向对象程序设计上机考试题库
一、 第一类题目 (20 道,每题 7 分,在 word 中保留代码并将输出结果窗口保留)
1.定义盒子 Box 类,要求具有以下成员:长、宽、高分别为 x,y,z ,可设置盒子形状;
可计算盒子体积;可计算盒子的表面积。
#includeiostream
class Box
{ private:
int x,y,z; int v,s;
public:
void int(int x1=0,int y1=0,int z1=0) {x=x1;y=y1;z=z1;}
void volue() {v=x*y*z;}
void area() {s=2*(x*y+x*z+y*z);}
void show()
{coutx= x y= y z=zendl;
couts= s v= vendl;
}
};
void main()
{ Box a;
a.init(2,3,4);
a.volue();
a.area();
a.show();
}
2.有两个长方柱,其长、宽、高分别为:( 1)30,20,10;(2 )12,10,20。分别求
他们的体积。编一个基于对象的程序,在类中用带参数的构造函数。
#include iostream
using namespace std;
class Box
{public:
Box(int,int,int);// 带参数的构造函数
int volume();
private:
int length;
int width;
int height;
};
Box::Box(int len,int h,int w)
{length=len;
height=h;
width=w;
【第1 页共 48 页】
}
//Box::Box(int len,int w,int,h):length(len),height(h),width(w){}
int Box::volume()
{return(length*width*height); }
int main()
{
Box box1(30,20,10);
coutThe volume of box1 is box1.volume()endl;
Box box2(12,10,20);
coutThe volume of box2 is box2.volume()endl;
return 0;
}
3.有两个长方柱,其长、宽、高分别为:( 1)12,20,25 ;(2 )10,30,20。分别求
他们的体积。编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参
数。
#include iostream
using namespace std;
class Box
{public:
Box();
Box(int len,int w ,int h):length(len),width(w),height(h){}
int volume();
private:
int length;
int width;
int height;
};
int Box::volume()
{return(length*width*height);
}
int main()
{
显示全部