多态性和虚函数.doc_第1页
多态性和虚函数.doc_第2页
多态性和虚函数.doc_第3页
多态性和虚函数.doc_第4页
多态性和虚函数.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Problem A: C+习题 抽象基类Description编写一个程序,声明抽象基类Shape,由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数printArea分别输出以上三者的面积(结果保留两位小数),3个图形的数据在定义对象时给定。Input圆的半径矩形的边长三角形的底与高Output圆的面积矩形的面积三角形的面积Sample Input12.64.5 8.44.5 8.4Sample Outputarea of circle = 498.76area of rectangle = 37.80area of triangle = 18.90#include #include 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:public Shapepublic: Rectangle(double w,double h):width(w),height(h) virtual double area() const return width*height; protected: double width,height;class Triangle:public Shapepublic: Triangle(double w,double h):width(w),height(h) virtual double area()const return 0.5*width*height; protected: double width,height;void printArea(const Shape &s) couts.area()endl;int main() float r,a,b,w,h; coutfixedr; Circle circle(r); coutab; Rectangle rectangle(a,b); coutwh; Triangle triangle(w,h); coutarea of triangle = ; printArea(triangle); return 0;Problem B: C+习题 虚函数-计算图形面积Description编写一个程序,定义抽象基类Shape,由它派生出5个派生类: Circle(圆形)、Square(正方形)、Rectangle(矩形)、Trapezoid(梯形)、Triangle(三角形)。用虚函数分别计算几种图形面积,并求它们之和。要求用基类指针数组,使它每一个元素指向一个派生类对象。Input圆的半径正方形的边长矩形的边长梯形的上底和下底,高三角形的底与高Output所有图形面积之和(结果保留两位小数)Sample Input12.63.54.5 8.42.0 4.5 3.24.5 8.4Sample Outputtotal of all areas=574.11#include #include 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 Square:public Shapepublic: Square(double s):side(s) virtual double area() const return side*side; protected: double side;class Rectangle:public 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 t,double b,double h):top(t),bottom(b),height(h) virtual double area()const return 0.5*(top+bottom)*height; protected: double top,bottom,height;class Triangle:public Shapepublic: Triangle(double w,double h):width(w),height(h) virtual double area()const return 0.5*width*height; protected: double width,height;int main() float r,a,b,w1,w2,w,h; coutfixedr; Circle circle(r); cina; Square square(a); cinab; Rectangle rectangle(a,b); cinw1w2h; Trapezoid trapezoid(w1,w1,h); cinwh; Triangle triangle(w,h); Shape *pt5= &circle,&square,&rectangle,&trapezoid,▵ double areas=0.0; for(int i=0; iarea(); couttotal of all areas=areasendl; return 0;Problem C: 抽象一个形状类Description阮宝同学期待着暑假来临,知道C+不好好复习麻烦不小。没有多态性,那就不叫面向对象,老师不划重点也能猜到。嘿嘿,自己做个经典题,怎么变题也不怕。老湿,再难的题还有木有?Input输入四个数,前两个是矩形的长和宽,后两个是三角形的底边长和高。Output分两行输出两个数,第一个是矩形的面积,第二个是三角形的面积。Sample Input3.5 6.43.5 6.4Sample Output22.411.2#include using namespace std;class Shapepublic: virtual double area()const=0;class Rectangle:public Shapepublic: Rectangle(double w,double h):width(w),height(h) virtual double area() const return width*height; protected: double width,height;class Triangle:public Shapepublic: Triangle(double w,double h):width(w),height(h) virtual double area()const return 0.5*width*height; protected: double width,height;int main() double a,b; cinab; /输入矩形的长和宽 Rectangle r(a,b);/建立Rectangle类对象r, 矩形长a宽b Shape *s1=&r; coutarea()wh; /输入矩形的长和宽 Triangle t(w,h); /建立Triangle类对象t,三角形底边长w高h Shape &s2=t; couts2.area();/输出三角形面积 return 0;Problem D: 虚函数练习:动物2Description长期的物种进化使自然界出现了生活在陆地上的陆生动物和生活在水中的水生动物。根据已有主函数编写动物类,陆生动物类和水生动物类。Input动物的体长,体重,性别;水生动物的体长,体重,性别,游泳速度;陆生动物的体长,体重,性别,奔跑速度;Output动物的体长,体重,性别;水生动物的体长,体重,性别,游泳速度;陆生动物的体长,体重,性别,奔跑速度;Sample Input52 22 f62 32 m 12272 42 m 102Sample Outputheight:52weight:22sex:fheight:62weight:32sex:mswimming_speed:122height:72weight:42sex:mrunning_speed:102#include using namespace std; class animal public: animal(int h,int w,char s)height=h;weight=w;sex=s; virtual void display() coutheight:heightendl; coutweight:weightendl; coutsex:sexendl; protected: int height; int weight; char sex; ; class aqu_animal:public animal public: aqu_animal(int h,int w,char s,int s_p):animal(h,w,s),swimming_speed(s_p) virtual void display() coutheight:heightendl; coutweight:weightendl; coutsex:sexendl; coutswimming_speed:swimming_speedendl; protected: int swimming_speed; ; class ter_animal:public animal public: ter_animal(int h,int w,char s,int r_p):animal(h,w,s),running_speed(r_p) virtual void display() coutheight:heightendl; coutweight:weightendl; coutsex:sexendl; coutrunning_speed:running_speedabc; animal pa(a,b,c); p=&pa; p-display(); cinabcs; aqu_animal pb(a,b,c,s); p=&pb; p-display(); cinabcr; ter_animal pc(a,b,c,r); p=&pc; p-display(); return 0; Problem E: 交通工具信息Description有一个交通工具类vehicle,将它为 基类派生的有派生小车类car,卡车类truck和轮船类boat,定义这些类,并使其能够显示其各类交通工具的详细信息。他们包含的信息,有如下几种:1.名字 -与输入的名字相符合2.时速(km/h) -最高时速3.耗油量(L/100km) -在经济时速下测得耗油量小车类:250km/h14.8 L/100km卡车类:140km/h18L/100km轮船类:50km/h8.33L/100kmInput输入三个派生类对应名字。Output输出对应类型的信息。Sample InputMercedes_BenzTransport_truckBoatSample OutputMercedes_Benz250km/h14.8 L/100kmTransport_truck140km/h18L/100kmBoat50km/h8.33L/100km#include using namespace std;class Vehicle public: virtual void showinfo()=0;class Car:public Vehiclepublic:Car(string n):name(n)virtual void showinfo()coutMercedes_Benzendl;cout250km/hn14.8 L/100kmendl;protected: string name;class Truck:public Vehiclepublic:Truck(string n):name(n)virtual void showinfo()coutTransport_truckendl;cout140km/hn18L/100kmendl;protected: st

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论