您好,欢迎访问三七文档
实验九多态性与虚函数(2)一、实验目的和要求1.了解多态的概念;2.了解虚函数的作用及使用方法;3.了解静态关联和动态关联的概念和用法;4.了解纯虚函数和抽象类的概念和用法二、实验内容和结果1.声明一个车(vehicle)基类,具有MaxSpeed、weight等成员变量有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)有座位数(SeatNum)等属性,类从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数,使用虚函数完成。(各个类都必须定义构造函数和析构函数)#includeiostreamusingnamespacestd;classvehicle{protected:floatMaxSpeed;floatweight;public:~vehicle(){}vehicle(floatm,floatw):MaxSpeed(m),weight(w){}virtualvoidRun(){cout车已经启动啦!!!endlendl;}virtualvoidStop(){coutvehicle已经停啦!!!endl;}};classbicycle:virtualpublicvehicle{protected:floatHeight;public:~bicycle(){}bicycle(floatm,floatw,floath):vehicle(m,w),Height(h){}voidRun(){coutTheheightofbicycleis:Heightendlendl;}voidStop(){coutbicycle已经停啦!!!endl;}};classmotorcar:virtualpublicvehicle{protected:intSeatNum;public:~motorcar(){}motorcar(floatm,floatw,floats):vehicle(m,w),SeatNum(s){}voidRun(){coutThenumberofmotorcar'sseatis:SeatNumendlendl;}voidStop(){coutmotorcar已经停啦!!!endl;}};classmotorcycle:publicbicycle,publicmotorcar{public:~motorcycle(){}motorcycle(floatm,floatw,floath,floats):vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s){}voidRun(){coutMaxSpeed:MaxSpeedendl;coutweight:weightendl;coutTheheightis:Heightendl;coutThenumberofseatis:SeatNumendl;}voidStop(){coutmotorcycle已经停啦!!!endl;}};intmain(){vehiclea(108,53);bicycleb(180,50,2);motorcarc(180,80,5);motorcycled(120,90,1.2,3);vehicle*p[4]={&a,&b,&c,&d};for(inti=0;i4;i++){p[i]-Stop();p[i]-Run();}return0;}2.定义抽象基类Shape,由它派生出3个派生类:Circle,Square,Rectangle,Triangle,Trapezoid(梯形)。用一个函数printArea分别输出以上五者的面积,5个图形的数据在定义对象时给出。#includeiostream#includeiomanipusingnamespacestd;classShape{public:~Shape(){}virtualvoidSetdata(float=0,float=0,float=0)=0;virtualfloatArea()=0;//求面积、纯虚函数};classTriangle:publicShape//三角形类{protected:floatW,H;public:~Triangle(){}Triangle(floatw=0,floath=0):W(w),H(h){}voidSetdata(floatw=0,floath=0,floatj=0){W=w;H=h;}floatArea(){returnW*H/2;}};classSquare:publicShape//正方形类{protected:floatS;public:~Square(){}Square(floats=0):S(s){}voidSetdata(floats=0,floath=0,floatj=0){S=s;}floatArea(){returnS*S;}};classRectangle:publicShape//矩形类{protected:floatL;floatW;public:~Rectangle(){}voidSetdata(floatl=0,floatw=0,floatj=0){L=l;W=w;}Rectangle(floatl=0,floatw=0):L(l),W(w){}floatArea(){returnL*W;}};classCircle:publicShape//圆类{protected:floatR;public:~Circle(){}Circle(floatr=0):R(r){}voidSetdata(floatr=0,floath=0,floatj=0){R=r;}floatArea(){return3.1415926*R*R;}};classTrapezoid:publicShape//梯形类{protected:floatSD;floatXD;floatH;public:~Trapezoid(){}Trapezoid(floatsd=0,floatxd=0,floath=0):SD(sd),XD(xd),H(h){}voidSetdata(floatsd=0,floatxd=0,floath=0){SD=sd;XD=xd;H=h;}floatArea(){return0.5*(SD+XD)*H;}};intmain(){coutsetiosflags(ios::fixed)setprecision(2);Trianglesjx;sjx.Setdata(2,3);//设置数据cout三角形面积为:sjx.Area()endl;Squarezfx(4);cout正方形面积为:zfx.Area()endl;Rectanglejx(2,4);cout矩形面积为:jx.Area()endl;Circleyuan(2);cout圆面积为:yuan.Area()endl;Trapezoidtx(2.5,3.5,6.5);tx.Setdata(1,2,3);//设置数据cout梯形面积为:tx.Area()endl;Shape*p[5]={&sjx,&zfx,&jx,&yuan,&tx};floatarea=0;for(inti=0;i5;i++)area+=p[i]-Area();cout总面积为:areaendl;return0;}3.实现计算器程序,要求输入两个数和运算符,得到结果,用简单工厂模式完成。#includeiostreamusingnamespacestd;classOperater{protected:inta,b,result;charop;public:Operater(intA,charOP,intB):a(A),b(B),op(OP){}Operater(){a=1,b=1,op='';}virtualintgetResult()=0;};classOperaterAdd:publicOperater{public:OperaterAdd(intA,charOP,intB):Operater(A,OP,B){}OperaterAdd():Operater(){}intgetResult(){returnresult=a+b;}};classOperaterSub:publicOperater{public:OperaterSub(intA,charOP,intB):Operater(A,OP,B){}OperaterSub():Operater(){}intgetResult(){returnresult=a-b;}};classOperaterMul:publicOperater{public:OperaterMul(intA,charOP,intB):Operater(A,OP,B){}OperaterMul():Operater(){}intgetResult(){returnresult=a*b;}};classOperaterDiv:publicOperater{public:OperaterDiv(intA,charOP,intB):Operater(A,OP,B){}OperaterDiv():Operater(){}intgetResult(){if(b!=0){result=a/b;returnresult;}elsecout除数不能为0!;}};classOperaterFactory{public:Operater*createOperate(charop){Operater*oper;switch(op){case'+':oper=newOperaterAdd();break;case'-':oper=newOperaterSub();break;case'*':oper=newOperaterMul();break;case'/':oper=newOperaterDiv();break;}returnoper;}};intmain(){OperaterFactoryOF;Operater*oper=OF.createOperate('+');coutoper-getResult()endl;return0;}
本文标题:声明一个车(vehicle)基类,具有MaxSpeed、weight等成员变量有Run、Stop等成
链接地址:https://www.777doc.com/doc-6371346 .html