版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、文档供参考,可复制、编制,期待您的好评与关注! C+面向对象程序设计上机考试题库一、第一类题目(20道,每题7分,在word中保留代码并将输出结果窗口保留)1定义盒子Box类,要求具有以下成员:长、宽、高分别为x,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的表面积。#include<iostream>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*(
2、x*y+x*z+y*z); void show() cout<<"x= "<<x<<" y= "<<y<<" z="<<z<<endl; cout<<"s= "<<s<<" v= "<<v<<endl; ;void main() Box a;a.init(2,3,4);a.volue();a.area();a.show();2 有两个长方柱,其长、宽、高
3、分别为:(1)30,20,10;(2)12,10,20。分别求他们的体积。编一个基于对象的程序,在类中用带参数的构造函数。 #include <iostream> using namespace std; class Box public: Box(int,int,int);/带参数的构造函数 int volume(); private: int length; int width; int height; ;Box:Box(int len,int h,int w) length=len; height=h; width=w; /Box:Box(int len,int w,int,
4、h):length(len),height(h),width(w)int Box:volume() return(length*width*height); int main() Box box1(30,20,10); cout<<"The volume of box1 is "<<box1.volume()<<endl; Box box2(12,10,20); cout<<"The volume of box2 is "<<box2.volume()<<endl; return 0
5、; 3 有两个长方柱,其长、宽、高分别为:(1)12,20,25;(2)10,30,20。分别求他们的体积。编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参数。 #include <iostream>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(
6、length*width*height); int main() Box box1(10,20,25); cout<<"The volume of box1 is "<<box1.volume()<<endl; Box box2(10,30,20); cout<<"The volume of box2 is "<<box2.volume()<<endl; return 0; 4 声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。#include <io
7、stream>using namespace std;template<class numtype>/声明一个类模板class Compare public: Compare(numtype a,numtype b) x=a;y=b; numtype max() return (x>y)?x:y; numtype min() return (x<y)?x:y; private: numtype x,y; ;int main()Compare<int> cmp1(3,7); cout<<cmp1.max()<<" is
8、the Maximum of two inteder numbers."<<endl; cout<<cmp1.min()<<" is the Minimum of two inteder numbers."<<endl<<endl; Compare<float> cmp2(45.78,93.6); cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl; cout&l
9、t;<cmp2.min()<<" is the Minimum of two float numbers."<<endl<<endl; Compare<char> cmp3('a','A'); cout<<cmp3.max()<<" is the Maximum of two characters."<<endl; cout<<cmp3.min()<<" is the Minimum of two c
10、haracters."<<endl; return 0; 5 建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。初值自拟。#include <iostream>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() cout<<num<<
11、;" "<<score<<endl; 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;i<=2;p=p+2,i+) p->display(); return 0; 6 建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成
12、绩最高者,并输出其学号。初值自拟。#include <iostream>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),Student(105,95.5); void max(Student* ); Student *p=&am
13、p;stud0; max(p); void max(Student *arr)float max_score=arr0.score; int k=0; for(int i=1;i<5;i+) if(arri.score>max_score) max_score=arri.score;k=i; cout<<arrk.num<<" "<<max_score<<endl; 7 用new建立一个动态一维数组,并初始化int10=1,2,3,4,5,6,7,8,9,10,用指针输出,最后销毁数组所占空间。#include&l
14、t;iostream>#include<string>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)<<" " cout<<endl; delete p; return;8 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。初值自拟。#include <iostr
15、eam>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 Complex:display(
16、)cout<<"("<<real<<","<<imag<<"i)"<<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; cout<<"c3
17、=" c3.display(); return 0;9 定义一个复数类Complex,重载运算符“”,“”,使之能用于复数的加,减运算,运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数之和,差。初值自拟。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); voi
18、d 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-c2.real; c.imag=imag-c2.imag; return c;void Complex:display()cout<<"(
19、"<<real<<","<<imag<<"i)"<<endl;int main()Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c1+c2=" c3.display(); c3=c1-c2; cout<<"c1-c2=" c3.display(); return 0;10 定义一个复数类Complex,重载运算符 “*”,“/”,使之能用于复数的乘,除。运算符重载函数作为Co
20、mplex类的成员函数。编程序,分别求出两个复数之积和商。初值自拟。提示:两复数相乘的计算公式为:(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 <iostream>using namespace std;class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator*(Comple
21、x &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; return c;Complex Complex:operator/(Complex &c2)Complex c; c.real=(real*c2.r
22、eal+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<<","<<imag<<"i)"<<endl;int main()Complex c1(3,4),c2(5,-10),c3; c
23、3=c1*c2; cout<<"c1*c2=" c3.display(); c3=c1/c2; cout<<"c1/c2=" c3.display(); return 0;11 定义一个复数类Complex,重载运算符“”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。编程序,分别求两个复数之和、整数和复数之和。初值自拟。#include <iostream.h> class Complex p
24、ublic: 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(); private: double real; double imag; ; Complex Complex:operator+(Complex &c)return Complex(re
25、al+c.real,imag+c.imag); Complex Complex:operator+(int &i)return Complex(real+i,imag);void Complex:display()cout<<"("<<real<<","<<imag<<"i)"<<endl;Complex operator+(int &i,Complex &c)return Complex(i+c.real,c.imag);int main
26、()Complex c1(3,4),c2(5,-10),c3; int i=5; c3=c1+c2; cout<<"c1+c2=" c3.display(); c3=i+c1; cout<<"i+c1=" c3.display(); c3=c1+i; cout<<"c1+i=" c3.display(); return 0;12 有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如c=a+b。初值自拟。#include <iostream.h>clas
27、s Matrix public: Matrix(); friend Matrix operator+(Matrix &,Matrix &); void input(); void display(); private: int mat23; ;Matrix:Matrix() for(int i=0;i<2;i+) for(int j=0;j<3;j+) matij=0;Matrix operator+(Matrix &a,Matrix &b) Matrix c; for(int i=0;i<2;i+) for(int j=0;j<3;j+
28、) c.matij=a.matij+b.matij; return c; void Matrix:input() cout<<"input value of matrix:"<<endl; for(int i=0;i<2;i+) for(int j=0;j<3;j+) cin>>matij;void Matrix:display() for (int i=0;i<2;i+) for(int j=0;j<3;j+) cout<<matij<<" " cout<<
29、endl;int main()Matrix a,b,c; a.input(); b.input(); cout<<endl<<"Matrix a:"<<endl; a.display(); cout<<endl<<"Matrix b:"<<endl; b.display(); c=a+b; cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl; c.display(); ret
30、urn 0;13 将运算符“”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为Complex类的友元函数。初值自拟。#include <iostream.h>class Complex public: Complex()real=0;imag=0; Complex(double r)real=r;imag=0; Complex(double r,double i)real=r;imag=i; friend Complex operator+ (Complex &c1,Complex &c2); void display(); private: doub
31、le real; double imag; ;Complex operator+ (Complex &c1,Complex &c2) return Complex(c1.real+c2.real, c1.imag+c2.imag); void Complex:display()cout<<"("<<real<<","<<imag<<"i)"<<endl;int main()Complex c1(3,4),c2(5,-10),c3; c3=c1+c2
32、; cout<<"c1=" c1.display(); cout<<"c2=" c2.display(); cout<<"c1+c2=" c3.display(); return 0;14. 定义一个字符串类String,用来存放不定长的字符串,重载运算符“”,用于两个字符串的等于比较运算。初值自拟。#include <iostream.h>#include <string.h>class String public: String()p=NULL; String(char
33、*str); friend bool operator=(String &string1,String &string2); void display(); private: char *p; ; String:String(char *str)p=str;void String:display() cout<<p; bool operator=(String &string1,String &string2)if(strcmp(string1.p,string2.p)=0) return true; else return false;void c
34、ompare(String &string1,String &string2)if(operator=(string1,string2)=1) string1.display();cout<<"="string2.display(); cout<<endl;int main()String string1("Hello"),string2("Hello"); compare(string1,string2); return 0;15. 定义一个字符串类String,用来存放不定长的字符串,重载运
35、算符"<",用于两个字符串的小于的比较运算。初值自拟。#include <iostream.h>#include <string.h>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
36、() cout<<p; 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(); co
37、ut<<endl;int main()String string1("Book"),string2("Computer"); compare(string1,string2); return 0;16 定义一个字符串类String,用来存放不定长的字符串,重载运算符">",用于两个字符串的大于的比较运算。初值自拟。 #include <iostream.h>#include <string.h>class String public: String()p=NULL; String(char *
38、str); friend bool operator>(String &string1,String &string2); void display(); private: char *p; ; String:String(char *str)p=str;void String:display() cout<<p; bool operator>(String &string1,String &string2)if(strcmp(string1.p,string2.p)>0) return true; else return fals
39、e;void compare(String &string1,String &string2)if(operator>(string1,string2)=1) string1.display();cout<<">"string2.display(); cout<<endl;int main()String string1("Hello"),string2("Book"); compare(string1,string2); return 0;17 定义一个描述学生基本情况的类,数据成
40、员包括姓名、学号、C+成绩、英语和数学成绩,成员函数包括输出数据,求出总成绩和平均成绩。数据自拟。#include"string.h"#include <iostream.h>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
41、; fScore2 = s2; void print() cout<< cout<<"姓名:"<<strName;cout<<"学号:"<<strStuNO;cout<<" C+成绩:"<<fScore0<<"英语成绩:"<<fScore1<<"数学成绩:"<<fScore2<<endl; float GetSUM() return (float)(fS
42、core0 + 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;cout<<"姓名:"cin>>Name;cout<<"学号:"cin>>Stu
43、NO;cout<<" 成绩1:"<<" 成绩2: "<<" 成绩3: "<<"n"cin>>a>>b>>c;one.SetScore(Name,StuNO,a,b,c);one.print();cout<<"平均成绩为 "<<one.GetAverage()<<"n"cout<<"总成绩"<<one.GetSUM(
44、)<<"n"18 先建立一个Point(点)类,包含数据成员x,y(坐标点)。以它为基类,派生出一个Circle(圆)类,增加数据成员r(半径),再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,在增加数据成员h(高)。编写程序,重载运算符“<<”和“>>”,使之能够用于输出以上类对象。#include <iostream.h>class Pointpublic: Point(float=0,float=0); void setPoint(float,float); float getX() const r
45、eturn 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)output<<"
46、"<<p.x<<","<<p.y<<""<<endl; 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 Circl
47、e &); protected: float radius;Circle:Circle(float a,float b,float r):Point(a,b),radius(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
48、)output<<"Center="<<c.x<<","<<c.y<<", r="<<c.radius<<", area="<<c.area()<<endl; return output;class Cylinder:public Circlepublic: Cylinder (float x=0,float y=0,float r=0,float h=0); void setHeight(float); f
49、loat 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:getHeig
50、ht() const return height;float Cylinder:area() const return 2*Circle:area()+2*3.14159*radius*height;float Cylinder:volume() constreturn Circle:area()*height;ostream &operator<<(ostream &output,const Cylinder& cy)output<<"Center="<<cy.x<<","<
51、<cy.y<<", r="<<cy.radius<<", h="<<cy.height <<"narea="<<cy.area()<<", volume="<<cy.volume()<<endl; return output;int main()Cylinder cy1(3.5,6.4,5.2,10); cout<<"noriginal cylinder:nx="<
52、<cy1.getX()<<", y="<<cy1.getY()<<", r=" <<cy1.getRadius()<<", h="<<cy1.getHeight()<<"narea="<<cy1.area() <<", volume="<<cy1.volume()<<endl; cy1.setHeight(15); cy1.setRadius(7.5); cy
53、1.setPoint(5,5); cout<<"nnew cylinder:n"<<cy1; Point &pRef=cy1; cout<<"npRef as a point:"<<pRef; Circle &cRef=cy1; cout<<"ncRef as a Circle:"<<cRef; return 0;19 写一个程序,定义抽象类型Shape,由他派生三个类:Circle(圆形),Rectangle(矩形),Trapezoid(梯形),
54、用一个函数printArea分别输出三者的面积,3个图形的数据在定义对象是给定。#include <iostream>using namespace std;class Shapepublic: virtual double area() const =0; ;class Circle:public Shapepublic:Circle(double r):radius(r) virtual double area() const return 3.14159*radius*radius; protected: double radius; ;class Rectangle:pub
55、lic Shapepublic: Rectangle(double w,double h):width(w),height(h) virtual double area() const return width*height; protected: double width,height; ;class Trapezoid:public Shapepublic: Trapezoid(double w,double h,double len):width(w),height(h),length(len) virtual double area() const return 0.5*height*
56、(width+length); protected: double width,height,length; ;void printArea(const Shape &s)cout<<s.area()<<endl; int main() Circle circle(12.6); cout<<"area of circle =" printArea(circle); Rectangle rectangle(4.5,8.4); cout<<"area of rectangle =" printArea(
57、rectangle); Trapezoid trapezoid(4.5,8.4,8.0); cout<<"area of trapezoid =" printArea(trapezoid); return 0;20 定义一个人员类Cperson,包括数据成员:姓名、编号、性别和用于输入输出的成员函数。在此基础上派生出学生类CStudent(增加成绩)和老师类Cteacher(增加教龄),并实现对学生和教师信息的输入输出。 #include <iostream.h>#include <string.h>class CPersonpublic:void SetData(char *name, char *id, bool isman = 1) int n = strlen(name);strnc
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年黔东南客运从业资格证考试一点通
- 外贸行业劳动合同模板
- 合伙购买合同范例
- 剧组拍摄合同模板
- 商铺兼职电工合同范例
- 农业公司种子合同范例
- 住宅工程建设合同范例
- 供应链造价合同范例
- 员工融资合作合同范例
- 吊车承租合同范例
- 拉波夫叙事结构助力读后续写高考真题讲义-高三英语二轮复习写作专项
- 【失败案例】“疯太阳”郑州亚细亚集团的陨落亚细亚(战略和体制)
- 大学化学-物质结构基础
- 医院印章使用申请表
- 摩托车有限公司产品零部件编码原则与国内车型代码资料汇编
- 积极心理学视域下的班级文化建设
- 银行保险客户KYC基础信息表
- WINCC满足FDA规范配置说明分解
- 煤场机械车辆操作规程
- JJG 1030-2007超声流量计
- GB 30254-2013高压三相笼型异步电动机能效限定值及能效等级
评论
0/150
提交评论