C++面向对象程序设计上机考试题库(40页).doc
《C++面向对象程序设计上机考试题库(40页).doc》由会员分享,可在线阅读,更多相关《C++面向对象程序设计上机考试题库(40页).doc(40页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-C+面向对象程序设计上机考试题库-第 39 页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); vo
2、id 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 using namespace std; class Box public: Box(int,int,int);/带参数的构造函数 int volume(); private: int length; in
3、t width; int height;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
4、box2 is box2.volume()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:
5、volume() return(length*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: Compar
6、e(numtype a,numtype 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
7、.max() is the 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个学生的数据。
8、初值自拟。#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() 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)
9、; Student *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(1
10、02,85.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()rea
12、l=0;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 &c
13、2) 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 定义一个复数类Complex,重载运算符“”,“”,使之能用于复数的加,减运算,运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数之和,差。初值自拟。using namespace std;class Complex public: Complex()real=0
14、;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator+(Complex &c2); Complex operator-(Complex &c2); void display(); private: double real; double imag;Complex Complex:operator+(Complex &c2)Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c;Complex Complex:operator-(Complex &c
15、2)Complex c; c.real=real-c2.real; c.imag=imag-c2.imag; return c;void Complex:display()cout(real,imagi)endl;int main()Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; coutc1+c2=; c3.display(); c3=c1-c2; coutc1-c2=; c3.display(); return 0;10 定义一个复数类Complex,重载运算符 “*”,“/”,使之能用于复数的乘,除。运算符重载函数作为Complex类的成员函数。编程序,分
16、别求出两个复数之积和商。初值自拟。提示:两复数相乘的计算公式为:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i。两复数相除的计算公式为:(a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad) /(c*c+d*d)i。#include using namespace std;class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator*(Complex &c2); Complex operator/(Complex
17、 &c2); void display(); private: double real; double imag;Complex Complex:operator*(Complex &c2)Complex c; c.real=real*c2.real-imag*c2.imag; c.imag=imag*c2.real+real*c2.imag; return c;Complex Complex:operator/(Complex &c2)Complex c; c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag)
18、; c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); return c;void Complex:display()cout(real,imagi)endl;int main()Complex c1(3,4),c2(5,-10),c3; c3=c1*c2; coutc1*c2=; c3.display(); c3=c1/c2; coutc1/c2=; c3.display(); return 0;11 定义一个复数类Complex,重载运算符“”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对
19、象,也可以其中有一个是整数,顺序任意。例如:c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。编程序,分别求两个复数之和、整数和复数之和。初值自拟。#include class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator+(Complex &c2); Complex operator+(int &i); friend Complex operator+(int&,Complex &); void display(); pri
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 面向 对象 程序设计 上机 考试 题库 40
限制150内