多态性与虚函数.docx

上传人:b****1 文档编号:14865479 上传时间:2023-06-28 格式:DOCX 页数:12 大小:73.21KB
下载 相关 举报
多态性与虚函数.docx_第1页
第1页 / 共12页
多态性与虚函数.docx_第2页
第2页 / 共12页
多态性与虚函数.docx_第3页
第3页 / 共12页
多态性与虚函数.docx_第4页
第4页 / 共12页
多态性与虚函数.docx_第5页
第5页 / 共12页
多态性与虚函数.docx_第6页
第6页 / 共12页
多态性与虚函数.docx_第7页
第7页 / 共12页
多态性与虚函数.docx_第8页
第8页 / 共12页
多态性与虚函数.docx_第9页
第9页 / 共12页
多态性与虚函数.docx_第10页
第10页 / 共12页
多态性与虚函数.docx_第11页
第11页 / 共12页
多态性与虚函数.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

多态性与虚函数.docx

《多态性与虚函数.docx》由会员分享,可在线阅读,更多相关《多态性与虚函数.docx(12页珍藏版)》请在冰点文库上搜索。

多态性与虚函数.docx

多态性与虚函数

实验九多态性与虚函数

(2)

一、实验目的和要求

1.了解多态的概念;

2.了解虚函数的作用及使用方法;

3.了解静态关联和动态关联的概念和用法;

4.了解纯虚函数和抽象类的概念和用法

二、实验内容和结果

1.声明一个车(vehicle)基类,具有MaxSpeed、weight等成员变量有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)有座位数(SeatNum)等属性,类从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数,使用虚函数完成。

(各个类都必须定义构造函数和析构函数)

#include

usingnamespacestd;

classvehicle

{

protected:

floatMaxSpeed;

floatweight;

public:

~vehicle(){}

vehicle(floatm,floatw):

MaxSpeed(m),weight(w){}

virtualvoidRun()

{

cout<<"车已经启动啦!

"<

}

virtualvoidStop()

{

cout<<"vehicle已经停啦!

"<

}

};

classbicycle:

virtualpublicvehicle

{

protected:

floatHeight;

public:

~bicycle(){}

bicycle(floatm,floatw,floath):

vehicle(m,w),Height(h){}

voidRun()

{

cout<<"Theheightofbicycleis:

"<

}

voidStop()

{

cout<<"bicycle已经停啦!

"<

}

};

classmotorcar:

virtualpublicvehicle

{

protected:

intSeatNum;

public:

~motorcar(){}

motorcar(floatm,floatw,floats):

vehicle(m,w),SeatNum(s){}

voidRun()

{

cout<<"Thenumberofmotorcar'sseatis:

"<

}

voidStop()

{

cout<<"motorcar已经停啦!

"<

}

};

classmotorcycle:

publicbicycle,publicmotorcar

{

public:

~motorcycle(){}

motorcycle(floatm,floatw,floath,floats):

vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s){}

voidRun()

{

cout<<"MaxSpeed:

"<

cout<<"weight:

"<

cout<<"Theheightis:

"<

cout<<"Thenumberofseatis:

"<

}

voidStop()

{

cout<<"motorcycle已经停啦!

"<

}

};

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;i<4;i++)

{

p[i]->Stop();

p[i]->Run();

}

return0;

}

2.定义抽象基类Shape,由它派生出3个派生类:

Circle,Square,Rectangle,Triangle,Trapezoid(梯形)。

用一个函数printArea分别输出以上五者的面积,5个图形的数据在定义对象时给出。

#include

#include

usingnamespacestd;

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()

{

cout<

:

fixed)<

(2);

Trianglesjx;

sjx.Setdata(2,3);//设置数据

cout<<"三角形面积为:

"<

Squarezfx(4);

cout<<"正方形面积为:

"<

Rectanglejx(2,4);

cout<<"矩形面积为:

"<

Circleyuan

(2);

cout<<"圆面积为:

"<

Trapezoidtx(2.5,3.5,6.5);

tx.Setdata(1,2,3);//设置数据

cout<<"梯形面积为:

"<

Shape*p[5]={&sjx,&zfx,&jx,&yuan,&tx};

floatarea=0;

for(inti=0;i<5;i++)

area+=p[i]->Area();

cout<<"总面积为:

"<

return0;

}

3.实现计算器程序,要求输入两个数和运算符,得到结果,用简单工厂模式完成。

#include

usingnamespacestd;

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('+');

cout<getResult()<

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2