




已阅读5页,还剩36页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
类与对象1:Point类1Description定义一个Point类,数据成员包括私有数据成员为double类型的点坐标x,y;成员函数包括构造函数Point(用于实现对数据成员x,y的初始化),成员函数Display(用于输出点坐标x、y,输出格式为点坐标用逗号分隔并半角圆括号括起来)。main函数如下(不得修改main函数):int main() double x,y; cinxy; Point p1(x,y); p1.Display(); return 0;InputOutputSample Input12.5 22.7 Sample Output(12.5,22.7)*#includeusing namespace std;class Pointpublic: Point(double xx,double yy) x=xx;y=yy; void Display() cout(x,y)xy; Point p1(x,y); p1.Display(); return 0;类与对象2:Point类2Description定义一个Point类,数据成员包括私有数据成员为double类型的点坐标x,y;成员函数包括构造函数Point(用于实现对数据成员x,y的初始化)、成员函数Set(用于改变数据成员x、y的值)、成员函数Display(用于输出点坐标x、y,输出格式为点坐标用逗号分隔并半角圆括号括起来)。main函数如下(不得修改main函数):int main() double x1,y1,x2,y2; cinx1y1; cinx2y2; Point p1(x1,y1); p1.Display(); p1.Set(x2,y2); p1.Display(); return 0;InputOutputSample Input10 25.5 5.5 20Sample Output(10,25.5)(5.5,20)*#includeusing namespace std;class Pointpublic: Point(double xx,double yy) x=xx;y=yy; void Display() cout(x,y)x1y1; cinx2y2; Point p1(x1,y1); p1.Display(); p1.Set(x2,y2); p1.Display(); return 0;类与对象3:Point类3Description定义一个Point类,数据成员包括私有数据成员为double类型的点坐标x,y;成员函数包括构造函数Point(用于实现对数据成员x,y的初始化)、成员函数Set(用于改变数据成员x、y的值)、成员函数LeftMove(点坐标向左移动detax)、成员函数上移UpMove(点坐标上移detay)、成员函数Display(用于输出点坐标x、y,输出格式为点坐标用逗号分隔并半角圆括号括起来)。main函数如下(不得修改main函数):int main() double x1,y1,x2,y2,dx,dy; cinx1y1; cindx; cindy; cinx2y2; Point p1; p1.Display(); p1.Set(x1,y1); p1.Display(); p1.LeftMove(dx); p1.Display(); p1.UpMove(dy); p1.Display(); Point p2(x2,y2); p2.Display(); return 0;InputOutputSample Input10 25.5 5 10 5.5 20Sample Output(0,0)(10,25.5)(5,25.5)(5,35.5)(5.5,20)*#includeusing namespace std;class Pointpublic: Point(double xx=0,double yy=0) x=xx;y=yy; void Display() cout(x,y)x1y1; cindx; cindy; cinx2y2; Point p1; p1.Display(); p1.Set(x1,y1); p1.Display(); p1.LeftMove(dx); p1.Display(); p1.UpMove(dy); p1.Display(); Point p2(x2,y2); p2.Display(); return 0;类与对象4:Rectangle类1Description定义一个长方形类Rectangle,私有数据成员为double型width、height(表示长方形的宽和高),成员函数包括构造函数Rectangle(用于实现对数据成员width、height的初始化)、成员函数GetArea(计算并返回长方形的面积)。main函数如下(不得修改main函数):int main() double width,height; cinwidthheight; Rectangle r1; coutr1.GetArea()endl; Rectangle r2(width,height); coutr2.GetArea()endl; return 0;InputOutputSample Input10.2 25.5Sample Output0260.1*#includeusing namespace std;class Rectanglepublic: Rectangle(double w=0,double h=0); double GetArea();private: double width,height;Rectangle:Rectangle(double w,double h) width=w;height=h;double Rectangle:GetArea() return width*height;int main() double width,height; cinwidthheight; Rectangle r1; coutr1.GetArea()endl; Rectangle r2(width,height); coutr2.GetArea()widthheight; cinn; Rectangle r1; coutr1.GetArea()endl; Rectangle r2(width,height); coutr2.GetArea()endl; r2.Expand(n); coutr2.GetArea()endl; return 0;InputOutputSample Input10.2 25.5 3Sample Output100260.12340.9*#includeusing namespace std;class Rectanglepublic: Rectangle(double w=10,double h=10); double GetArea(); void Expand(int n);private: double width,height;Rectangle:Rectangle(double w,double h) width=w;height=h;double Rectangle:GetArea() return width*height;void Rectangle:Expand(int n) width*=n;height*=n;int main() double width,height; int n; cinwidthheight; cinn; Rectangle r1; coutr1.GetArea()endl; Rectangle r2(width,height); coutr2.GetArea()endl; r2.Expand(n); coutr2.GetArea()endl; return 0;类与对象6:Rectangle类3Description定义一个长方形类Rectangle,私有数据成员为double型width、height表示长方形的宽和高),成员函数包括构造函数Rectangle(用于实现对数据成员width、height的初始化,默认宽和高都为10)、成员函数GetArea(计算并返回长方形的面积)。main函数中输入两组宽和高,用于实例化两个类对象R1,R2,判断两个长方形R1、R2面积的大小。 程序执行时:输入1.5 1.1 3.3 2输出1.651.98InputOutputSample Input3.5 2.0 1.8 1.1Sample Output71.98*#includeusing namespace std;class Rectanglepublic: Rectangle(double w=10,double h=10); double GetArea(); void Expand(int n);private: double width,height;Rectangle:Rectangle(double w,double h) width=w;height=h;double Rectangle:GetArea() return width*height;void Rectangle:Expand(int n) width*=n;height*=n;int main() double w1,h1,w2,h2,s1,s2; cinw1h1w2h2; Rectangle r1(w1,h1),r2(w2,h2); s1=r1.GetArea(); s2=r2.GetArea(); couts2) cout; else if(s1s2) cout; else cout=; couts2endl; return 0;类与对象7:Triangle类1Description定义一个三角形类Triangle,包括三个double型数据成员a、b和c表示三角形的三条边,成员函数包括构造函数、IsTriangle、GetArea。构造函数用于实现对数据成员的初始化;成员函数IsTriangle判断三条边是否构成三角形,成员函数GetArea返回三角形的面积。main函数中输入两组三角形的边长,用这两组边长实例化两个三角形类对象T1,T2,若T1、T2均能构成三角形,则输出两个三角形面积之和,否则输出failure。输入:3 4 5 6 8 10 输出:30输入:3 4 5 6 7 8 输出:26.3332输入:1 2 0 3 4 5 输出:failureInputOutputSample Input3 4 5 6 7 8Sample Output26.3332*#include#includeusing namespace std;class Trianglepublic: Triangle(double aa,double bb,double cc); bool IsTriangle(); double GetArea();private: double a,b,c;Triangle:Triangle(double aa,double bb,double cc) a=aa;b=bb;c=cc;bool Triangle:IsTriangle() if(a+bc&a+cb&b+ca) return true; else return false;double Triangle:GetArea() double p=(a+b+c)/2; return sqrt(p*(p-a)*(p-b)*(p-c);int main() double a1,b1,c1,a2,b2,c2; cina1b1c1a2b2c2; Triangle T1(a1,b1,c1),T2(a2,b2,c2); if(T1.IsTriangle()&T2.IsTriangle() coutT1.GetArea()+T2.GetArea()endl; else coutfailureendl; return 0;类与对象8:Triangle类2Description定义一个三角形类Triangle,包括三个double型数据成员a、b和c表示三角形的三条边,成员函数包括构造函数、IsTriangle、GetPerimeter。构造函数用于实现对数据成员的初始化;成员函数IsTriangle判断三条边是否构成三角形,成员函数GetPerimeter返回三角形的周长。main函数中输入两组三角形的边长,用这两组边长实例化两个三角形类对象T1,T2,若T1、T2均能构成三角形,则输出两个三角形周长之差,否则输出failure。输入:3 4 5 6 8 10 输出: -12输入:3 4 5 6 7 8 输出:-9输入:1 2 0 3 4 5 输出:failureInputOutputSample Input3 4 5 6 7 7.5Sample Output-8.5*#include#includeusing namespace std;class Trianglepublic: Triangle(double aa,double bb,double cc); bool IsTriangle(); double GetArea(); double GetPerimeter();private: double a,b,c;Triangle:Triangle(double aa,double bb,double cc) a=aa;b=bb;c=cc;bool Triangle:IsTriangle() if(a+bc&a+cb&b+ca) return true; else return false;double Triangle:GetArea() double p=(a+b+c)/2; return sqrt(p*(p-a)*(p-b)*(p-c);double Triangle:GetPerimeter() return a+b+c;int main() double a1,b1,c1,a2,b2,c2; cina1b1c1a2b2c2; Triangle T1(a1,b1,c1),T2(a2,b2,c2); if(T1.IsTriangle()&T2.IsTriangle() coutT1.GetPerimeter()-T2.GetPerimeter()endl; else coutfailureendl; return 0;类与对象9:Cylinder类Description定义一个圆柱类Cylinder,成员数据为底圆半径r和圆柱高h,成员函数为构造函数,GetArea计算圆柱体的表面积,GetVolume计算圆柱体的体积。(注意值取3.14)main函数输入圆柱的半径和高,定义一个类对象,计算并输出圆柱的表面积和体积。InputOutputSample Input1.5 1.5Sample Output28.26 10.5975*#includeusing namespace std;const double PI=3.14;class Cylinderprivate:double r;double h;public:Cylinder(double rr,double hh)r=rr;h=hh;double GetArea()return 2*PI*r*r+2*PI*r*h;double GetVolume()return PI*r*r*h;int main()double rr,hh;cinrrhh;Cylinder a(rr,hh);couta.GetArea() a.GetVolume()endl;return 0;类与对象10:Person类Description设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写构造函数Person,实现数据成员初始化;编写拷贝构造函数;编写Display函数显示数据成员信息;编写析构函数。main函数中利用Person类构造函数建立类对象p1,再利用拷贝构造函数建立对象p2,打印每个person类对象的信息。InputOutput要求:两个对象的数据分行显示Sample Inputliming 12Sample Outputliming 12liming 12*#include #include using namespace std; class Person private: char *name; int id; public: Person(char *name1,int id1); Person(Person &p); void Display(); Person(); Person:Person(char *name1,int id1) int len=strlen(name1); name=new charlen+1; strcpy(name,name1); id=id1; Person:Person(Person &p) int len=strlen(); name=new charlen+1; strcpy(name,); id=p.id; void Person:Display() coutname idnameid; Person p1(name,id); p1.Display(); Person p2=p1; p2.Display(); return 0; 类与对象11:Complex类1Description定义一个复数类Complex,该类对象存放一个复数的实部和虚部。设计带有默认形参值的构造函数,实部虚部的默认值均为0;设计一个能够输出复数的Display成员函数(当虚部为0时仅输出实部;输出样式如:18.5、10+3i、10-3.5i、-5.8+9.5i、-4.5-9.6i等)。main函数如下(不得修改main函数):int main() double r1,i1; cinr1i1; Complex c1(r1,i1); c1.Display(); return 0;InputOutputSample Input2.5 -3.8Sample Output2.5-3.8i*#includeusing namespace std;class Complexprivate:double real;double imag;public:Complex(double r,double i)real=r;imag=i;void Display()if(real!=0) cout0)cout+;if(imag!=0) coutimagi;if(real=0&imag=0) coutr1i1; Complex c1(r1,i1); c1.Display();return 0;类与对象12:Array类1Description数组类Array用于存储一组双精度浮点数,其定义如下,给出各个成员函数实现。const int MaxSize=100; /数组中元素的最大个数class Arraypublic: Array (); /初始化length=0 int Length(); /返回数组中元素实际个数 void Insert(int i, double x); /在下标i处插入x void Display(); /输出数组中实际元素 private: double dataMaxSize; /存储元素 int length; /数组中实际元素个数;main函数中输入n个(n=100)双精度浮点数(以0结束),将这n个数按输入次序存储到同一个Array类对象的数据成员data中(利用成员函数Insert实现),并调用Display输出这些数。InputOutputSample Input35 3 64 7 6443 0Sample OutputLength:5Elements:35 3 64 7 6443*#includeusing namespace std;const int MaxSize=100; /数组中元素的最大个数class Arraypublic: Array (); /初始化length=0 int Length(); /返回数组中元素实际个数 void Insert(int i, double x); /在下标i处插入x void Display(); /输出数组中实际元素 private: double dataMaxSize; /存储元素 int length; /数组中实际元素个数;Array:Array()length=0;void Array:Display()int i;for(i=0;ilength;i+)coutdatai ;cout=i;j-) dataj+1=dataj; datai=x; length+;int Array:Length()return length;int main()Array a;double x;int i;cinx;while(x)i=a.Length ();a.Insert(i,x);cinx;coutLength:a.Length()endl;coutElements:;a.Display();return 0;类与对象13:Array类2Description数组类Array用于存储一组双精度浮点数,在现有类定义基础适当增加必要的成员函数,完成下列任务:main函数中输入n个(nnum; if(!num) break; try a.Insert(i+,num); /insert catch(char *ms) coutmsx; try int n=a.Delete(x); /delete all x coutCount of deleted elements:nendl; catch(char *ms) coutmsendl; a.Sort(); /sort a.Display(); return 0;InputOutputSample Input23 2 2 344 3 32 2 5 0 2Sample OutputLength:8Elements:23 2 2 344 3 32 2 5 Count of deleted elements:3Length:5Elements:3 5 23 32 344 *#include using namespace std;const int MaxSize=100;class Arraypublic: Array (); int Length(); void Insert(int i, double x); void Display(); void Sort(); int Delete(double x);private: double dataMaxSize; int length;Array:Array() length=0;int Array:Length() return length;void Array:Sort()int i,j; for(i=0;ilength-1;i+) for(j=i+1;jlength;j+) if(dataj=i;j-) dataj+1=dataj; datai=x; length+;int Array:Delete(double x) int n=0,i,j;j=0; for(i=0;ilength;i+) if(datai!=x) dataj=datai; j+; n=length-j; length=j; return n;void Array:Display() int i;coutLength:lengthendl; coutElements:; for(i=0;ilength;i+) coutdatai ; coutnum; if(!num) break; try a.Insert(i+,num); catch(char *ms) coutmsx; try int n=a.Delete(x); coutCount of deleted elements:nendl; catch(char *ms) coutmsendl; a.Sort(); a.Display(); return 0;类与对象14:Array类3Description数组类Array用于存储一组双精度浮点数,其定义如下,给出各个成员函数实现。class Arraypublic:Array (int size=100); /初始化:MaxSize置为size,为data动态分配内存,length置为0 Array (const Array &a); /深拷贝构造函数 Array (); /析构函数 void Insert(int i, double x); /在下标i处插入x void Display(); /输出数组中的元素 private: int MaxSize; /数组中可存储的元素最大个数 double *data; /存储元素
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025江苏徐州鼓楼区招聘公益性岗位人员16人考试模拟试题及答案解析
- 2025广东佛山市华英三水学校招聘语文合同制教师1人考试模拟试题及答案解析
- 2025年海洋科技领域成果转化专项资金申报指南报告
- 合肥市城市社区文化养老模式与发展路径探析
- 押题宝典教师招聘之《小学教师招聘》通关考试题库附答案详解(培优)
- 演出经纪人之《演出经纪实务》试题预测试卷有答案详解
- 2025呼伦贝尔农垦集团有限公司社会招聘50人考试备考附答案详解(突破训练)
- 教师招聘之《小学教师招聘》考前冲刺测试卷(培优b卷)附答案详解
- 2025年教师招聘之《小学教师招聘》考前冲刺练习题库【历年真题】附答案详解
- 2025年教师招聘之《小学教师招聘》考前冲刺模拟题库附参考答案详解(轻巧夺冠)
- 融资风险评估报告
- 画法几何及土木工程制图课件
- 第2课 树立科学的世界观《哲学与人生》(高教版2023基础模块)
- 录入与排版教学计划
- 2023免拆底模钢筋桁架楼承板图集
- 云计算技术基础应用教程(HCIA-Cloud)PPT完整全套教学课件
- 呼吸衰竭小讲课课件
- 成人学士学位英语1000个高频必考词汇汇总
- 全屋定制家居橱柜衣柜整装安装服务规范
- 沥青及沥青混合料试验作业指导书
- 义务教育阶段学生艺术素质测评指标体系小学音乐
评论
0/150
提交评论