多态性和虚函数.docx
- 文档编号:5071030
- 上传时间:2022-12-13
- 格式:DOCX
- 页数:15
- 大小:17.75KB
多态性和虚函数.docx
《多态性和虚函数.docx》由会员分享,可在线阅读,更多相关《多态性和虚函数.docx(15页珍藏版)》请在冰豆网上搜索。
多态性和虚函数
ProblemA:
C++习题抽象基类
Description
编写一个程序,声明抽象基类Shape,由它派生出3个派生类:
Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数printArea分别输出以上三者的面积(结果保留两位小数),3个图形的数据在定义对象时给定。
Input
圆的半径
矩形的边长
三角形的底与高
Output
圆的面积
矩形的面积
三角形的面积
SampleInput
12.6
4.58.4
4.58.4
SampleOutput
areaofcircle=498.76
areaofrectangle=37.80
areaoftriangle=18.90
#include
#include
usingnamespacestd;
classShape
{
public:
virtualdoublearea()const=0;
};
classCircle:
publicShape
{
public:
Circle(doubler):
radius(r){}
virtualdoublearea()const
{
return3.14159*radius*radius;
};
protected:
doubleradius;
};
classRectangle:
publicShape
{
public:
Rectangle(doublew,doubleh):
width(w),height(h){}
virtualdoublearea()const
{
returnwidth*height;
}
protected:
doublewidth,height;
};
classTriangle:
publicShape
{
public:
Triangle(doublew,doubleh):
width(w),height(h){}
virtualdoublearea()const
{
return0.5*width*height;
}
protected:
doublewidth,height;
};
voidprintArea(constShape&s)
{
cout< } intmain() { floatr,a,b,w,h; cout< (2); cin>>r; Circlecircle(r); cout<<"areaofcircle="; printArea(circle); cin>>a>>b; Rectanglerectangle(a,b); cout<<"areaofrectangle="; printArea(rectangle); cin>>w>>h; Triangletriangle(w,h); cout<<"areaoftriangle="; printArea(triangle); return0; } ProblemB: C++习题虚函数-计算图形面积 Description 编写一个程序,定义抽象基类Shape,由它派生出5个派生类: Circle(圆形)、Square(正方形)、Rectangle(矩形)、Trapezoid(梯形)、Triangle(三角形)。 用虚函数分别计算几种图形面积,并求它们之和。 要求用基类指针数组,使它每一个元素指向一个派生类对象。 Input 圆的半径 正方形的边长 矩形的边长 梯形的上底和下底,高 三角形的底与高 Output 所有图形面积之和(结果保留两位小数) SampleInput 12.6 3.5 4.58.4 2.04.53.2 4.58.4 SampleOutput totalofallareas=574.11 #include #include usingnamespacestd; classShape { public: virtualdoublearea()const=0; }; classCircle: publicShape { public: Circle(doubler): radius(r){} virtualdoublearea()const { return3.14159*radius*radius; }; protected: doubleradius; }; classSquare: publicShape { public: Square(doubles): side(s){} virtualdoublearea()const { returnside*side; } protected: doubleside; }; classRectangle: publicShape { public: Rectangle(doublew,doubleh): width(w),height(h){} virtualdoublearea()const { returnwidth*height; } protected: doublewidth,height; }; classTrapezoid: publicShape { public: Trapezoid(doublet,doubleb,doubleh): top(t),bottom(b),height(h){} virtualdoublearea()const { return0.5*(top+bottom)*height; } protected: doubletop,bottom,height; }; classTriangle: publicShape { public: Triangle(doublew,doubleh): width(w),height(h){} virtualdoublearea()const { return0.5*width*height; } protected: doublewidth,height; }; intmain() { floatr,a,b,w1,w2,w,h; cout< (2); cin>>r; Circlecircle(r); cin>>a; Squaresquare(a); cin>>a>>b; Rectanglerectangle(a,b); cin>>w1>>w2>>h; Trapezoidtrapezoid(w1,w1,h); cin>>w>>h; Triangletriangle(w,h); Shape*pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle}; doubleareas=0.0; for(inti=0;i<5;i++) areas=areas+pt[i]->area(); cout<<"totalofallareas="< return0; } ProblemC: 抽象一个形状类 Description 阮宝同学期待着暑假来临,知道C++不好好复习麻烦不小。 没有多态性,那就不叫面向对象,老师不划重点也能猜到。 嘿嘿,自己做个经典题,怎么变题也不怕。 老湿,再难的题还有木有? Input 输入四个数,前两个是矩形的长和宽,后两个是三角形的底边长和高。 Output 分两行输出两个数,第一个是矩形的面积,第二个是三角形的面积。 SampleInput 3.56.4 3.56.4 SampleOutput 22.4 11.2 #include usingnamespacestd; classShape { public: virtualdoublearea()const=0; }; classRectangle: publicShape { public: Rectangle(doublew,doubleh): width(w),height(h){} virtualdoublearea()const { returnwidth*height; } protected: doublewidth,height; }; classTriangle: publicShape { public: Triangle(doublew,doubleh): width(w),height(h){} virtualdoublearea()const { return0.5*width*height; } protected: doublewidth,height; }; intmain() { doublea,b; cin>>a>>b;//输入矩形的长和宽 Rectangler(a,b);//建立Rectangle类对象r,矩形长a宽b Shape*s1=&r; cout< doublew,h; cin>>w>>h;//输入矩形的长和宽 Trianglet(w,h);//建立Triangle类对象t,三角形底边长w高h Shape&s2=t; cout< return0; } ProblemD: 虚函数练习: 动物2 Description 长期的物种进化使自然界出现了生活在陆地上的陆生动物和生活在水中的水生动物。 根据已有主函数编写动物类,陆生动物类和水生动物类。 Input 动物的体长,体重,性别; 水生动物的体长,体重,性别,游泳速度; 陆生动物的体长,体重,性别,奔跑速度; Output 动物的体长,体重,性别; 水生动物的体长,体重,性别,游泳速度; 陆生动物的体长,体重,性别,奔跑速度; SampleInput 5222f 6232m122 7242m102 SampleOutput height: 52 weight: 22 sex: f height: 62 weight: 32 sex: m swimming_speed: 122 height: 72 weight: 42 sex: m running_speed: 102 #include usingnamespacestd; classanimal { public: animal(inth,intw,chars) { height=h; weight=w; sex=s; } virtualvoiddisplay() { cout<<"height: "< cout<<"weight: "< cout<<"sex: "< } protected: intheight; intweight; charsex; }; classaqu_animal: publicanimal { public: aqu_animal(inth,intw,chars,ints_p): animal(h,w,s),swimming_speed(s_p){} virtualvoiddisplay() { cout<<"height: "< cout<<"weight: "< cout<<"sex: "< cout<<"swimming_speed: "< } protected: intswimming_speed; }; classter_animal: publicanimal { public: ter_animal(inth,intw,chars,intr_p): animal(h,w,s),running_speed(r_p){} virtualvoiddisplay() { cout<<"height: "< cout<<"weight: "< cout<<"sex: "< cout<<"running_speed: "< } protected: intrunning_speed; }; intmain() { inta,b,s,r; charc; animal*p; cin>>a>>b>>c; animalpa(a,b,c); p=&pa; p->display(); cin>>a>>b>>c>>s; aqu_animalpb(a,b,c,s); p=&pb; p->display(); cin>>a>>b>>c>>r; ter_animalpc(a,b,c,r); p=&pc; p->display(); return0; } ProblemE: 交通工具信息 Description 有一个交通工具类vehicle,将它为基类派生的有派生小车类car,卡车类truck和轮船类boat,定义这些类,并使其能够显示其各类交通工具的详细信息。 他们包含的信息,有如下几种: 1.名字 -----与输入的名字相符合 2.时速(km/h) -----最高时速 3.耗油量(L/100km) -----在经济时速下测得耗油量 小车类: 250km/h 14.8L/100km 卡车类: 140km/h 18L/100km 轮船类: 50km/h 8.33L/100km Input 输入三个派生类对应名字。 Output 输出对应类型的信息。 SampleInput Mercedes_Benz Transport_truck Boat SampleOutput Mercedes_Benz 250km/h 14.8L/100km Transport_truck 140km/h 18L/100km Boat 50km/h 8.33L/100km #include usingnamespacestd; classVehicle { public: virtualvoidshowinfo()=0; }; classCar: publicVehicle { public: Car(stringn): name(n){} virtualvoidshowinfo() { cout<<"Mercedes_Benz"< cout<<"250km/h"<<'\n'<<"14.8L/100km"< } protected: stringname; }; classTruck: publicVehicle { public: Truck(stringn): name(n){} virtualvoidshowinfo() { cout<<"Transport_truck"< cout<<"140km/h"<<'\n'<<"18L/100km"< } protected: stringname; }; classBoat: publicVehicle { public: Boat(stringn): name(n){} virtualvoidshowinfo() { cout<<"Boat"< cout<<"50km/h"<<'\n'<<"8.33L/100km"< } protected: stringname; }; intmain() { Vehicle*vp; stringname0,name1,name2; cin>>name0; Carcar(name0); cin>>name1; Trucktruck(name1); cin>>name2; Boatboat(name2); vp=&car; vp->showinfo(); vp=&truck; vp->showinfo(); vp=&boat; vp->showinfo(); return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 多态性 函数