实现原理:虚函数
h
/******************************************** CopyRight 200 北京交通大学 程序名称: 图形管理容器 文件名: GraphManage.h 修改日期: 2007-4-7 19:31:01 描述: 定义了容器类及相关图形类 ********************************************/ #include <iostream> #include <vector> using namespace std; //基态类 Container class Container { public: Container(int ID,int Type); int virtual GetArea()=0; int virtual GetGirth()=0; virtual ~Container(){}; void ShowInfo(); int GetId(){return m_nID;} protected: int m_nID; int m_nType; }; //直线类 Line class Line:public Container { public: Line(int ID); int GetArea(); int GetGirth(); protected: int m_nLength; }; //三角类 Triangle class Triangle:public Container { public: Triangle(int ID); int GetArea(); int GetGirth(); protected: int m_a,m_b,m_c; }; //椭圆类型 Ellipse class Ellipse:public Container { public: Ellipse(int ID); int GetGirth(); int GetArea(); protected: int m_a,m_b; }; //矩阵类 Matrix class Matrix:public Container { public: Matrix(int ID); int GetArea(); int GetGirth(); protected: int m_a,m_b; }; //容器管理类 Manager class Manager { private: vector<Container*> m_vecGraph; int m_num; public: Manager():m_num(0){}; void AddGraph(int type); void DelGraph(int ID); void Clear(); void ShowAll(); int Get_Num(){return m_num;}; };
cpp
/******************************************** CopyRight 2007 北京交通大学 程序名称: 图形管理容器 文件名: GraphManage.cpp 修改日期: 2007-4-7 20:13:48 描述: 菜单实现及容器测试主程序 ********************************************/ #include <iostream> #include "GraphManage.h" #include <list> #include <cmath> using namespace std; void Container::ShowInfo() { cout<<"编号:"<<m_nID<<endl<<"类型:"; switch(m_nType) { case 1: cout<<"直线"; break; case 2: cout<<"三角形"; break; case 3: cout<<"矩形"; break; case 4: cout<<"椭圆"; break; } cout<<endl; cout<<"面积:"<<this->GetArea()<<endl<<"周长:"<<this->GetGirth()<<endl<<endl; } Container::Container(int ID,int Type) { this->m_nID=ID; this->m_nType=Type; } int Line::GetArea() { return 0; } int Line::GetGirth() { return this->m_nLength; } Line::Line(int ID): Container(ID,1) { int Length=0; do { cout<<"请输入直线长度:"; cin>>Length; }while(Length<=0); this->m_nLength=Length; } Triangle::Triangle(int ID) :m_a(0),m_b(0),m_c(0),Container(ID,2) { cout<<"请依次输入三边长度:"; cin>>m_a>>m_b>>m_c; } Triangle::GetArea() { double s=static_cast<double>((m_a+m_b+m_c)/2); return sqrt(s*(s-m_a)*(s-m_b)*(s-m_c)); } Triangle::GetGirth() { return m_a+m_b+m_c; } Matrix::Matrix(int ID) :m_a(0),m_b(0),Container(ID,3) { do { cout<<"请输入两个边长:"; cin>>m_a>>m_b; }while(m_a<0||m_b<0); } int Matrix::GetArea() { return m_a*m_b; } int Matrix::GetGirth() { return (m_a+m_b)*2; } Ellipse::Ellipse(int ID) :m_a(0),m_b(0),Container(ID,4) { do{ cout<<"请输入椭圆的长半轴与短半轴长度:"; cin>>m_a>>m_b; }while(m_a<0||m_b<0||m_a<m_b); } int Ellipse::GetArea() { return 3.14*m_a*m_b; } int Ellipse::GetGirth() { return 2*3.14*m_b+4*(m_a-m_b); } void Manager::AddGraph(int type) { Container *ptr=NULL; switch(type) { case 1: ptr=new Line(++this->m_num); break; case 2: ptr=new Triangle(++this->m_num); break; case 3: ptr=new Matrix(++this->m_num); break; case 4: ptr=new Ellipse(++this->m_num); break; } if(ptr) { this->m_vecGraph.push_back(ptr); cout<<"成功写入容器!"<<endl; } else cout<<"内存分配失败!"<<endl; } void Manager::ShowAll() { cout<<endl<<"--------------"<<endl<<"开始遍历容器:"<<endl<<endl; vector<Container*>::iterator itCur=m_vecGraph.begin(); while(itCur!=m_vecGraph.end()) { (*itCur)->ShowInfo(); itCur++; } cout<<"遍历结束!\n--------------"<<endl; } void Manager::DelGraph(int ID) { vector<Container*>::iterator itCur=m_vecGraph.begin(); bool flag=false; while(itCur!=this->m_vecGraph.end()) { if((*itCur)->GetId()==ID) { flag=true; cout<<"成功从容器中清空"<<(*itCur)->GetId()<<"号图形"<<endl; delete (*itCur); itCur = m_vecGraph.erase(itCur); } else itCur++; } } void Manager::Clear() { vector<Container*>::iterator itCur=m_vecGraph.begin(); while(itCur!=this->m_vecGraph.end()) { if(*itCur) delete (*itCur); itCur++; } this->m_vecGraph.clear(); cout<<"清空完毕!"; }
main
/******************************************** CopyRight 2007 北京交通大学 程序名称: 图形管理容器 文件名: main.cpp 修改日期: 2007-4-7 19:29:32 描述: 测试容器类Manager及菜单实现 ********************************************/ #include <iostream> #include "GraphManage.h" using namespace std; class Menu { public: void Choice(); Menu():m_Choice(-1){}; ~Menu(){cout<<"感谢使用本容器!!";}; private: void Show(); int m_Choice; Manager m_Manage; }; void Menu::Choice() { while(m_Choice) { Show(); cin>>m_Choice; switch(m_Choice) { case 1: { int type=0; cout<<"请选择添加类型:"; cout<<"1.直线 2.三角形 3.矩形 4.椭圆形"<<endl; do { cin>>type; }while(type < 1 || type > 4); m_Manage.AddGraph(type); } break; case 2: { int id=0; do { cout<<"请输入1~"<<this->m_Manage.Get_Num()<<"之间的一个ID(0推出):"; cin>>id; if(id==0) break; }while(id>this->m_Manage.Get_Num()||id<1); if(id) this->m_Manage.DelGraph(id); else break; } break; case 3: this->m_Manage.Clear(); break; case 4: { this->m_Manage.ShowAll(); } break; case 5: m_Choice=0; } } } void Menu::Show() { cout<<"**容器测试程序***"<<endl; cout<<"1.为容器添加对象"<<endl; cout<<"2.删除容器中对象"<<endl; cout<<"3.清空容器"<<endl; cout<<"4.遍历容器"<<endl; cout<<"5.退出程序"<<endl; } int main() { cout<<"\n图形容器 \n\n"; Menu m1; m1.Choice(); return 0; }