C++面向对象程序设计上机考试题库 下载本文

内容发布更新时间 : 2024/5/22 7:00:52星期一 下面是文章的全部内容请认真阅读。

C++面向对象程序设计上机考试题库

一、第一类题目(20道,每题7分,在word中保留代码并将输出结果窗口保留) 1.定义盒子Box类,要求具有以下成员:长、宽、高分别为x,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的表面积。 #include 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()

{cout<<\ cout<<\ } };

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 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);

cout<<\Box box2(12,10,20);

cout<<\ return 0; }

3.有两个长方柱,其长、宽、高分别为:(1)12,20,25;(2)10,30,20。分别求他们的体积。编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参数。

#include

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() {

Box box1(10,20,25); cout<<\Box box2(10,30,20);

cout<<\ return 0; }

4.声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。

#include

【第2页共48页】

using namespace std;

template//声明一个类模板 class Compare {public:

Compare(numtype a,numtype b) {x=a;y=b;} numtype max()

{return (x>y)?x:y;} numtype min()

{return (x

numtype x,y; };

int main()

{Compare cmp1(3,7);

cout<

cout< cmp2(45.78,93.6);

cout<

cout< cmp3('a','A');

cout<

5.建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。初值自拟。

#include using namespace std; class Student {public:

Student(int n,double s):num(n),score(s){} void display(); private: int num;

double score; };

void Student::display()

{cout<

int main()

{Student stud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)};

【第3页共48页】