收藏 分销(赏)

C++面向对象程序的设计上机考试题库完整.docx

上传人:二*** 文档编号:4455104 上传时间:2024-09-23 格式:DOCX 页数:95 大小:48KB
下载 相关 举报
C++面向对象程序的设计上机考试题库完整.docx_第1页
第1页 / 共95页
亲,该文档总共95页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

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 z=

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

3、x(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()endl;

4、return 0; 3 有两个长方柱,其长、宽、高分别为:112,20,25;210,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

5、*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,numtype b) x=a;

6、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 Maximum of

7、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个学生的数据。初值自拟。#include using namespac

8、e 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 *p=stud; for(int

9、 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.5),Student(103,98.5)

10、, 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,用指针输出,最后销毁数组所占空间。#include#in

11、cludeusing 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;imag=0; Complex(double

12、 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

13、_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;imag=0; Complex(double r,

14、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 &c2)Complex c; c.real=real

15、-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 &c2); void display(); pri

17、vate: 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; return c;void Complex:display()cout(real,imagi)endl;int main()Complex c1(3,4),c2(5,-10),c3;

18、c3=c1*c2; coutc1*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)re

19、al=r;imag=i; Complex operator+(Complex &c2); Complex operator+(int &i); friend Complex operator+(int&,Complex &); 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 Complex(real+i,im

20、ag);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行3列。求两个矩

21、阵之和。重载运算符“+,使之能用于矩阵相加。如c=a+b。初值自拟。#include class Matrix public: Matrix(); friend Matrix operator+(Matrix &,Matrix &); void input(); void display(); private: int mat23; ;Matrix:Matrix() for(int i=0;i2;i+) for(int j=0;j3;j+) matij=0;Matrix operator+(Matrix &a,Matrix &b) Matrix c; for(int i=0;i2;i+) fo

22、r(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(); coutendlMatrix a:endl; a.displ

23、ay(); 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; Complex(double r,double i)real=r;imag=

24、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 main()Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; coutc1=

25、; c1.display(); coutc2=; c2.display(); coutc1+c2=; c3.display(); return 0;14. 定义一个字符串类String,用来存放不定长的字符串,重载运算符“,用于两个字符串的等于比拟运算。初值自拟。#include #include class String public: String()p=NULL; String(char *str); friend bool operator=(String &string1,String &string2); void display(); private: char *p; ;Str

26、ing:String(char *str)p=str;void String:display() coutp;bool operator=(String &string1,String &string2)if(strcmp(string1.p,string2.p)=0) return true; else return false;void compare(String &string1,String &string2)if(operator=(string1,string2)=1) string1.display();cout=;string2.display(); coutendl;int

27、 main()String string1(Hello),string2(Hello); compare(string1,string2); return 0;15. 定义一个字符串类String,用来存放不定长的字符串,重载运算符,用于两个字符串的小于的比拟运算。初值自拟。#include #include class String public: String()p=NULL; String(char *str); friend bool operator(String &string1,String &string2); void display(); private: char *p;

28、 ;String:String(char *str)p=str;void String:display() coutp;bool operator(String &string1,String &string2)if(strcmp(string1.p,string2.p)0) return true; else return false;void compare(String &string1,String &string2)if(operator(string1,string2)=1) string1.display();cout;string2.display(); cout,用于两个字符

29、串的大于的比拟运算。初值自拟。 #include #include class String public: String()p=NULL; String(char *str); friend bool operator(String &string1,String &string2); void display(); private: char *p; ;String:String(char *str)p=str;void String:display() cout(String &string1,String &string2)if(strcmp(string1.p,string2.p)0

30、) return true; else return false;void compare(String &string1,String &string2)if(operator(string1,string2)=1) string1.display();cout;string2.display(); coutendl;int main()String string1(Hello),string2(Book); compare(string1,string2); return 0;17 定义一个描述学生根本情况的类,数据成员包括、学号、C+成绩、英语和数学成绩,成员函数包括输出数据,求出总成绩

31、和平均成绩。数据自拟。#includestring.h#include class CStuScore public: char strName12; char strStuNO9; void SetScore( char sname12, char NO9,float s0, float s1, float s2) strcpy(strName, sname); strcpy(strStuNO, NO); fScore0 = s0; fScore1 = s1; fScore2 = s2; void print() cout cout:strName;cout学号:strStuNO;cout

32、C+成绩:fScore0英语成绩:fScore1数学成绩:fScore2endl; float GetSUM() return (float)(fScore0 + fScore1 + fScore2); float GetAverage(); private: float fScore3; ; float CStuScore:GetAverage() return (float)(fScore0 + fScore1 + fScore2)/3.0); void main()CStuScore one;float a,b,c; char Name12; char StuNO9;coutName;c

33、outStuNO;cout 成绩1: 成绩2: 成绩3: abc;one.SetScore(Name,StuNO,a,b,c);one.print();cout平均成绩为 one.GetAverage()n;cout总成绩one.GetSUM()n;18 先建立一个Point点类,包含数据成员x,y坐标点。以它为基类,派生出一个Circle圆类,增加数据成员r半径,再以Circle类为直接基类,派生出一个Cylinder圆柱体类,在增加数据成员h高。编写程序,重载运算符“,使之能够用于输出以上类对象。#include class Pointpublic: Point(float=0,float

34、=0); void setPoint(float,float); float getX() const return x; float getY() const return y; friend ostream & operator(ostream &,const Point &);protected: float x,y;Point:Point(float a,float b)x=a;y=b;void Point:setPoint(float a,float b)x=a;y=b;ostream & operator(ostream &output,const Point &p)outputp

35、.x,p.yendl; return output;class Circle:public Pointpublic: Circle(float x=0,float y=0,float r=0); void setRadius(float); float getRadius() const; float area () const; friend ostream &operator(ostream &,const Circle &); protected: float radius;Circle:Circle(float a,float b,float r):Point(a,b),radius(

36、r)void Circle:setRadius(float r)radius=r;float Circle:getRadius() const return radius;float Circle:area() constreturn 3.14159*radius*radius;ostream &operator(ostream &output,const Circle &c)outputCenter=c.x,c.y, r=c.radius, area=c.area()endl; return output;class Cylinder:public Circlepublic: Cylinde

37、r (float x=0,float y=0,float r=0,float h=0); void setHeight(float); float getHeight() const; float area() const; float volume() const; friend ostream& operator(ostream&,const Cylinder&); protected: float height;Cylinder:Cylinder(float a,float b,float r,float h) :Circle(a,b,r),height(h)void Cylinder:setHeight(float h)height=h;float Cylinder:getHeight() const return height;float Cylinder:area() const return 2*Circle:area()+2*3.14159*radius*height;float Cylinder:volume() constreturn Circle:

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 考试专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服