实验三_多态和模板

实验三_多态和模板

ID:37758090

大小:30.50 KB

页数:4页

时间:2019-05-30

实验三_多态和模板_第1页
实验三_多态和模板_第2页
实验三_多态和模板_第3页
实验三_多态和模板_第4页
资源描述:

《实验三_多态和模板》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、实验三多态与模板一、声明一个Shape抽象类,在此基础上派生出Rectangle和Circle类,二者都有GetArea()和GetPerim()函数,GetArea用来计算对象的面积,GetPerim用来计算对象的周长。封装上述各类,并编写测试程序。Shape.h的内容:classShape{public:Shape();virtualvoidGetArea();virtualvoidGetPerim();virtual~Shape();};Shape.cpp的内容:#include"Shape.h"Shape

2、::Shape(){}Shape::~Shape(){}voidShape::GetArea(){}voidShape::GetPerim(){}Circle.h的内容:#include"Shape.h"classCCircle:publicShape{public:virtualvoidGetPerim();virtualvoidGetArea();CCircle();virtual~CCircle();private:floatr;};Circle.cpp中的内容#include"Circle.h"#incl

3、ude"math.h"#include"iostream.h"CCircle::CCircle(){cout<<"请输入圆的半径:"<>r;}CCircle::~CCircle(){cout<<"Thankyouverymuch!";}voidCCircle::GetArea(){floats;floatpi=3.14;s=pi*r*r;cout<<"这个圆形的面积是:"<

4、r;cout<<"这个圆形的周长是:"<

5、ream.h"Rectangle::Rectangle(){cout<<"请输入矩形的长和宽:"<>a>>b;}Rectangle::~Rectangle(){cout<<"Thankyouverymuch!"<

6、}Shapetest.cpp中的内容:#include"Circle.h"#include"Rectangle.h"#include"iostream.h"intmain(){CCircleC;C.GetArea();C.GetPerim();RectangleR;R.GetArea();R.GetPerim();return0;}二、有一种栈,这种栈是按数组索引减小的方向增长的。初始时栈为空且top=21。往栈中压入3个字符后,索引top=18,栈顶元素为list[top]=C。在这个栈模型中,索引top在每次

7、push索引操作后减小,而在pop操作后则增大。用此模型实现stack类模板。并编写测试程序。templateclassTStack{public:TStack();~TStack();voidpush(Te);Tpop();Ttop()const;private:Titem[N];intSP;//栈顶指针};stack.cpp中的内容:#include"iostream.h"#include"stack.h"templateTStack::TStac

8、k(){SP=N-1;}templateTStack::~TStack(){}templatevoidTStack::push(Te){SP=SP-1;item[SP]=e;}templateTTStack::pop(){if(SP>20)cout<<"error.

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。