c++ simple class template example: Stack
2014-11-19 18:16
405 查看
main.cpp
Stack.h // Why there's no cpp file for Stack to hide implementation? See: /article/6726167.html
#include "Stack.h" #include <iostream> using namespace std; class Box { public: Box():data(0), ID(num++) { cout << "Box" << ID << " cons" << endl; } // Notice that copy constructor and operator= must be implemented in a pairwise way. // Because if you need Copy constructor, you almost definitely need to implement operator= Box(const Box ©): data(copy.data), ID(num++) { cout << "Box" << ID << " copy cons" << endl; } Box& operator=(const Box &b) { this->data = b.data; return *this; } ~Box() { cout << "Box" << ID << " des" << endl; } int data; private: static int num; const int ID; }; int Box::num = 1; int main() { Box b1,b2,b3; b1.data = 1; b2.data = 2; b3.data = 3; Stack<Box> bstack; bstack.push(b1); bstack.push(b2); bstack.push(b3); while (!bstack.empty()) { cout << bstack.top().data << endl; bstack.pop(); } return 0; }
Stack.h // Why there's no cpp file for Stack to hide implementation? See: /article/6726167.html
#ifndef STACK_H #define STACK_H #include <stdexcept> template <typename T> class Stack { public: Stack(); ~Stack(); /** Inserts a new element at the top of the stack, above its current top element. The content of this new element is initialized to a copy of val. @param val value to which the inserted element is initialized */ void push(const T &val); /** @return a reference to the top element in the stack */ T& top(); /** @return a const reference to the top element in the stack */ const T& top() const; /** Removes the element on top of the stack. This calls the removed element's destructor. */ void pop(); /** @return the number of elements in the stack. */ size_t size() const; bool empty() const; private: template <typename TYPE> class Link { public: TYPE data; Link *next; Link(const TYPE &_data, Link *_next = NULL): data(_data), next(_next) {} ~Link() { next = NULL; } }; Link<T> *head; size_t sz; // size, to avoid name conflict with Stack::size() }; template <typename T> Stack<T>::Stack(): head(NULL), sz(0) {} template <typename T> Stack<T>::~Stack() { Link<T> *ptr = head; while (ptr != NULL) { ptr = head->next; delete head; head = ptr; } sz = 0; } /** Inserts a new element at the top of the stack, above its current top element. The content of this new element is initialized to a copy of val. @param val value to which the inserted element is initialized */ template <typename T> void Stack<T>::push(const T &val) { head = new Link<T>(val, head); ++sz; } /** @return a reference to the top element in the stack */ template <typename T> T& Stack<T>::top() { if (head == NULL) throw std::runtime_error("empty stack"); return head->data; } /** @return a const reference to the top element in the stack */ template <typename T> const T& Stack<T>::top() const { if (head == NULL) throw std::runtime_error("empty stack"); return head->data; } /** Removes the element on top of the stack. This calls the removed element's destructor. */ template <typename T> void Stack<T>::pop() { if (head == NULL) throw std::runtime_error("empty stack"); Link<T> *ptr = head->next; delete head; head = ptr; --sz; } /** @return the number of elements in the stack. */ template <typename T> size_t Stack<T>::size() const { return sz; } template <typename T> bool Stack<T>::empty() const { return (sz == 0); } #endif // STACK_H
相关文章推荐
- A Simple C++ Template Class that Matches a String to a Wildcard Pattern
- C++ class template(simple and clear definition)
- An easy instance of using the template class in C++
- Stack-c++template
- c++ 模板<template class T> c++ 模板<template class T>
- nullnullC++ Stack Example Rearranging RailRoad Cars 火车车厢重排问题
- C++ Stack Example Rearranging RailRoad Cars 火车车厢重排问题
- C++ Template 中的typename、class关键字
- C++ Knowledge series Template & Class
- c++的template中的class和typename .
- C++ template class
- C++ template<typename T> 和 template<class T>
- Stack-c++ template
- C++ Template 中的typename、class关键字
- c++的template中的class和typename
- Stack Using a C++ Template:
- How to Generate C++ Class Template Definitions
- C++ Template 中的typename、class关键字
- C#/C++ interop: A simple example
- C++ Template 中的typename、class关键字