《C++高级4模板.pptVIP

  • 16
  • 0
  • 约 49页
  • 2016-12-28 发布于北京
  • 举报
编程示例: 栈模板 #include iostream.h enum bool {False, True}; template typename T //抽象栈类模板定义 class AbsStack { // 是抽象基类,仅提供了接口 protected: unsigned height; // 栈顶“指针” public: bool IsEmpty( ) //测试栈是否为空 {return (height==0)?True:False;} virtual void push(const T )=0; //纯虚函数 virtual T pop( )=0; virtual void Empty( )=0; //置栈为空 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. virtual void ReadTop(T )=0; }; //使用数组存储数据元素的栈模板(又叫基于数组的栈) template class T, int Size //模板类派生出模板子类 class ArrayStack:public AbsStackT{ bool Error; // 栈满标志 T vec[Size]; // “基于数组” public: ArrayStack( ) { Empty( ); } virtual void push(const T); virtual T pop( ); virtual void Empty( ) { height=0; } virtual void ReadTop(T ); bool IsError( ) { return Error; } }; Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. //压栈操作() template class T, int Size void ArrayStackT, Size::push(const T elem) { Error=False; if(height==(Size)) { Error=True; return; } vec[height]=elem; //元素入栈 height++; // 栈顶“指针”上移 cout“调用了入栈操作。“elemendl; } Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. //出栈操作 template class T, int Size T ArrayStackT, Size::pop( ) { if(height==0) { Error=True; return NULL; } T elem=vec[height–1]; height – –; return elem; } Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. //读取栈顶元素 template class T, int Size void ArrayStackT, Size::ReadTop(T elem) { if(height==0) { Error=True; return; } elem=vec[height–1]; } Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. void main() { int j; ArrayStackint,

文档评论(0)

1亿VIP精品文档

相关文档