C++面向对象程序设计上机考试题库 .pdf
《C++面向对象程序设计上机考试题库 .pdf》由会员分享,可在线阅读,更多相关《C++面向对象程序设计上机考试题库 .pdf(48页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、【第 1 页共 48 页】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()coutx=
2、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;int width;int height;Box:Box(in
3、t len,int h,int w)length=len;height=h;width=w;名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 48 页 -【第 2 页共 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
4、volume of 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:v
5、olume()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 名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 48 页 -【第 3 页共 48 页】using namespace std;te
6、mplate/声明一个类模板class Compare public:Compare(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
7、 cmp2(45.78,93.6);coutcmp2.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 个学生的数据(学号、成绩),用指针指向数组首元
8、素,输出第 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()coutnum scoreendl;int main()Student stud5=Student(101,78.5),Student(102,85.5),Student(103,98.5),Student(104,100.0),Studen
9、t(105,95.5);名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 48 页 -【第 4 页共 48 页】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 sc
10、ore;void main()Student stud5=Student(101,78.5),Student(102,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
11、建立一个动态一维数组,并初始化int10=1,2,3,4,5,6,7,8,9,10,用指针输出,最后销毁数组所占空间。#include#include using namespace std;void main()int*p;p=new int10;for(int i=1;i=10;i+)*(p+i-1)=i;cout*(p+i-1);名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 48 页 -【第 5 页共 48 页】coutendl;delete p;return;8定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普
12、通函数。编写程序,求两个复数之和。初值自拟。#include using namespace std;class Complex public:Complex()real=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 Com
13、plex: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定义一个复数类Complex,重载运算符“”,“”,使之能用于复数的加,减运算,运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数
14、之和,差。初值自拟。using namespace std;名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 48 页 -【第 6 页共 48 页】class Complex public:Complex()real=0;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+(Com
15、plex&c2)Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;Complex Complex:operator-(Complex&c2)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
16、.display();return 0;10定义一个复数类Complex,重载运算符“*”,“/”,使之能用于复数的乘,除。运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数之积和商。初值自拟。提示:两复数相乘的计算公式为:(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;名师资料总结-精品资料
17、欢迎下载-名师精心整理-第 6 页,共 48 页 -【第 7 页共 48 页】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-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag;r
18、eturn c;Complex Complex:operator/(Complex&c2)Complex c;c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);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*
19、c2=;c3.display();c3=c1/c2;coutc1/c2=;c3.display();return 0;11定义一个复数类Complex,重载运算符“”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如: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;Comple
20、x operator+(Complex&c2);Complex operator+(int&i);friend Complex operator+(int&,Complex&);名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 48 页 -【第 8 页共 48 页】void display();private:double real;double imag;Complex Complex:operator+(Complex&c)return Complex(real+c.real,imag+c.imag);Complex Complex:operator+(int&i)return
21、 Complex(real+i,imag);void Complex:display()cout(real,imagi)endl;Complex operator+(int&i,Complex&c)return Complex(i+c.real,c.imag);int main()Complex c1(3,4),c2(5,-10),c3;int i=5;c3=c1+c2;coutc1+c2=;c3.display();c3=i+c1;couti+c1=;c3.display();c3=c1+i;coutc1+i=;c3.display();return 0;12有两个矩阵 a 和 b,均为 2
22、 行 3 列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如 c=a+b。初值自拟。#include class Matrix public:Matrix();friend Matrix operator+(Matrix&,Matrix&);void input();void display();private:int mat23;Matrix:Matrix()名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共 48 页 -【第 9 页共 48 页】for(int i=0;i2;i+)for(int j=0;j3;j+)matij=0;Matrix operator+(Ma
23、trix&a,Matrix&b)Matrix c;for(int i=0;i2;i+)for(int j=0;j3;j+)c.matij=a.matij+b.matij;return c;void Matrix:input()coutinput value of matrix:endl;for(int i=0;i2;i+)for(int j=0;jmatij;void Matrix:display()for(int i=0;i2;i+)for(int j=0;j3;j+)coutmatij;coutendl;int main()Matrix a,b,c;a.input();b.input();
24、coutendlMatrix a:endl;a.display();coutendlMatrix b:endl;b.display();c=a+b;coutendlMatrix c=Matrix a+Matrix b:endl;c.display();return 0;13将运算符“”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为 Complex类的友元函数。初值自拟。#include class Complex public:Complex()real=0;imag=0;Complex(double r)real=r;imag=0;名师资料总结-精品资料欢迎下载-名师精心整理
25、-第 9 页,共 48 页 -【第 10 页共 48 页】Complex(double r,double i)real=r;imag=i;friend Complex operator+(Complex&c1,Complex&c2);void display();private:double real;double imag;Complex operator+(Complex&c1,Complex&c2)return Complex(c1.real+c2.real,c1.imag+c2.imag);void Complex:display()cout(real,imagi)endl;int m
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+面向对象程序设计上机考试题库 2022 C+ 面向 对象 程序设计 上机 考试 题库
限制150内