您的位置:首页 > 编程语言 > C语言/C++

c++ simple class template example: Stack

2014-11-19 18:16 405 查看
main.cpp

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: