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

上传人:简****9 文档编号:99364381 上传时间:2019-09-18 格式:DOC 页数:48 大小:57.11KB
返回 下载 相关 举报
c++面向对象程序设计上机考试题库_第1页
第1页 / 共48页
c++面向对象程序设计上机考试题库_第2页
第2页 / 共48页
c++面向对象程序设计上机考试题库_第3页
第3页 / 共48页
c++面向对象程序设计上机考试题库_第4页
第4页 / 共48页
c++面向对象程序设计上机考试题库_第5页
第5页 / 共48页
点击查看更多>>
资源描述

《c++面向对象程序设计上机考试题库》由会员分享,可在线阅读,更多相关《c++面向对象程序设计上机考试题库(48页珍藏版)》请在金锄头文库上搜索。

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

2、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 using namespace std; class Box public: Box(int,int,int);/带参数的构造函数 int volume(); private: int length; int width; int height; ;

3、Box:Box(int len,int h,int w) length=len; height=h; width=w; /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(

4、)endl; 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(l

5、ength*width*height); int main() Box box1(10,20,25); coutThe volume of box1 is box1.volume()endl; Box box2(10,30,20); coutThe volume of box2 is box2.volume()endl; return 0; 4 声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。#include using namespace std;template/声明一个类模板class Compare public: Compare(numtype a,num

6、type b) x=a;y=b; numtype max() return (xy)?x:y; numtype min() return (xy)?x:y; private: numtype x,y; ;int main()Compare cmp1(3,7); coutcmp1.max() is the Maximum of two inteder numbers.endl; coutcmp1.min() is the Minimum of two inteder numbers.endlendl; Compare cmp2(45.78,93.6); coutcmp2.max() is the

7、 Maximum of two float numbers.endl; coutcmp2.min() is the Minimum of two float numbers.endlendl; Compare cmp3(a,A); coutcmp3.max() is the Maximum of two characters.endl; coutcmp3.min() is the Minimum of two characters.endl; return 0; 5 建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。初值自拟。#includ

8、e 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() coutnum scoreendl; int main()Student stud5= Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5); Student

9、 *p=stud; for(int i=0;idisplay(); return 0; 6 建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。初值自拟。#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) int num; float score; ;void main()Student stud5= Student(101,78.5),Student(102,85.

10、5),Student(103,98.5), Student(104,100.0),Student(105,95.5); void max(Student* ); Student *p=&stud0; max(p); void max(Student *arr)float max_score=arr0.score; int k=0; for(int i=1;imax_score) max_score=arri.score;k=i; coutarrk.num max_scoreendl; 7 用new建立一个动态一维数组,并初始化int10=1,2,3,4,5,6,7,8,9,10,用指针输出,最

11、后销毁数组所占空间。#include#includeusing namespace std;void main() int *p; p=new int10; for(int i=1;i=10;i+) *(p+i-1)=i; cout*(p+i-1) ; coutendl; delete p; return;8 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。初值自拟。#include using namespace std;class Complex public: Complex()real=0;

12、imag=0; Complex(double r,double i)real=r;imag=i; double get_real(); double get_imag(); void display(); private: double real; double imag; ; double Complex:get_real()return real;double Complex:get_imag()return imag;void Complex:display()cout(real,imagi)endl;Complex operator + (Complex &c1,Complex &c2) return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag();int main()Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; coutc3=; c3.display(); return 0;9 定义一个

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号