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

上传人:s9****2 文档编号:560547116 上传时间:2024-01-07 格式:DOCX 页数:43 大小:46KB
返回 下载 相关 举报
C++面向对象程序设计上机考试题库_第1页
第1页 / 共43页
C++面向对象程序设计上机考试题库_第2页
第2页 / 共43页
C++面向对象程序设计上机考试题库_第3页
第3页 / 共43页
C++面向对象程序设计上机考试题库_第4页
第4页 / 共43页
C++面向对象程序设计上机考试题库_第5页
第5页 / 共43页
点击查看更多>>
资源描述

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

1、-C+面向对象程序设计上机考试题库一、第一类题目20道,每题7分,在word中保存代码并将输出结果窗口保存1定义盒子Bo*类,要求具有以下成员:长、宽、高分别为*,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的外表积。*includeclass Bo* private: int *,y,z; int v,s; public: void int(int *1=0,int y1=0,int z1=0) *=*1;y=y1;z=z1; void volue() v=*y*z; void area() s=2*(*y+*z+y*z); void show() cout*= * y= y z=ze

2、ndl; couts= s v= vendl; ;void main() Bo* a;a.init(2,3,4);a.volue();a.area();a.show();2 有两个长方柱,其长、宽、高分别为:130,20,10;212,10,20。分别求他们的体积。编一个基于对象的程序,在类中用带参数的构造函数。 *include using namespace std; class Bo* public: Bo*(int,int,int);/带参数的构造函数 int volume(); private: int length; int width; int height; ;Bo*:Bo*(

3、int len,int h,int w) length=len; height=h; width=w; /Bo*:Bo*(int len,int w,int,h):length(len),height(h),width(w)int Bo*:volume() return(length*width*height); int main() Bo* bo*1(30,20,10); coutThe volume of bo*1 is bo*1.volume()endl;Bo* bo*2(12,10,20); coutThe volume of bo*2 is bo*2.volume()endl; re

4、turn 0; 3 有两个长方柱,其长、宽、高分别为:112,20,25;210,30,20。分别求他们的体积。编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参数。 *include using namespace std;class Bo* public: Bo*(); Bo*(int len,int w ,int h):length(len),width(w),height(h) int volume(); private: int length; int width; int height; ;int Bo*:volume() return(length*width*h

5、eight); int main() Bo* bo*1(10,20,25); coutThe volume of bo*1 is bo*1.volume()endl;Bo* bo*2(10,30,20); coutThe volume of bo*2 is bo*2.volume()endl; return 0; 4 声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。*include using namespace std;template/声明一个类模板class pare public: pare(numtype a,numtype b) *=a;y=b; num

6、type ma*() return (*y)?*:y; numtype min() return (*y)?*:y; private: numtype *,y; ;int main()pare cmp1(3,7); coutcmp1.ma*() is the Ma*imum of two inteder numbers.endl; coutcmp1.min() is the Minimum of two inteder numbers.endlendl; pare cmp2(45.78,93.6); coutcmp2.ma*() is the Ma*imum of two float numb

7、ers.endl; coutcmp2.min() is the Minimum of two float numbers.endlendl; pare cmp3(a,A); coutcmp3.ma*() is the Ma*imum of two characters.endl; coutcmp3.min() is the Minimum of two characters.endl; return 0;5 建立一个对象数组,放5个学生的数据*、成绩,用指针指向数组首元素,输出第1,3,5个学生的数据。初值自拟。*include using namespace std;class Studen

8、t 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 *p=stud; for(int i=0;idisplay(); r

9、eturn 0; 6 建立一个对象数组,放5个学生的数据*、成绩,设立一个函数ma*,用指向对象的指针作函数参数,在ma*函数中找出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.5),Student(103,98.5), Student(104,100.0)

10、,Student(105,95.5); void ma*(Student* ); Student *p=&stud0; ma*(p); void ma*(Student *arr)float ma*_score=arr0.score; int k=0; for(int i=1;ima*_score) ma*_score=arri.score;k=i; coutarrk.num ma*_scoreendl;7 用new建立一个动态一维数组,并初始化int10=1,2,3,4,5,6,7,8,9,10,用指针输出,最后销毁数组所占空间。*include*includeusing namespace

11、 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 定义一个复数类ple*,重载运算符“+,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。初值自拟。*include using namespace std;class ple* public: ple*()real=0;imag=0; ple*(double r,double i)real=r;imag=i; doubl

12、e get_real(); double get_imag(); void display(); private: double real; double imag; ;double ple*:get_real()return real;double ple*:get_imag()return imag;void ple*:display()cout(real,imagi)endl;ple* operator + (ple* &c1,ple* &c2) return ple*(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag();int main()ple* c1(3,4),c2(5,-10),c3; c3=c1+c2; coutc3=; c3.display(); return 0;9 定义一个复数类ple*,重载运算符“,“,使之能用于复数的加,减运算,运算符重载函数作为ple*类的成员函数。编程序,分别求出两个复数之和,差。初值自拟。using namespace std;class ple* public: ple*()real=0;im

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

当前位置:首页 > 高等教育 > 研究生课件

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