欢迎来到天天文库
浏览记录
ID:37758090
大小:30.50 KB
页数:4页
时间:2019-05-30
《实验三_多态和模板》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
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::TStac8、k(){SP=N-1;}templateTStack::~TStack(){}templatevoidTStack::push(Te){SP=SP-1;item[SP]=e;}templateTTStack::pop(){if(SP>20)cout<<"error.
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::TStac8、k(){SP=N-1;}templateTStack::~TStack(){}templatevoidTStack::push(Te){SP=SP-1;item[SP]=e;}templateTTStack::pop(){if(SP>20)cout<<"error.
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::TStac8、k(){SP=N-1;}templateTStack::~TStack(){}templatevoidTStack::push(Te){SP=SP-1;item[SP]=e;}templateTTStack::pop(){if(SP>20)cout<<"error.
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.
此文档下载收益归作者所有