codice:
//Pila.h
template <class T>
class Pila{
public:
Pila(int = MAXD);
Pila(const Pila<T>&);
~Pila();
bool push(const T&);
bool pop(T&);
bool top(T&);
bool piena();
bool vuota();
void reset();
/* Pila operator = (Pila);
Pila operator = (const Pila&); */
private:
int cima;
T *dati;
int MAX;
static const int MAXD;
};
codice:
//Pila.cpp
#include <assert.h>
#include "Pila.h"
template <class T>
Pila<T>::Pila()
{
MAX = MAXD;
cima = 0;
dati = new (nothrow) T[MAX];
assert(dati != NULL);
}
template <class T>
Pila<T>::Pila(int d)
{
assert(d>0);
MAX = d;
cima = 0;
dati = new (nothrow) T[MAX];
assert(dati != NULL);
}
template <class T>
Pila<T>::~Pila()
{
delete dati[];
}
template <class T>
Pila<T>::Pila(const Pila& p)
{
this->cima = p.cima;
this->MAX = p.MAX;
this->dati = new (nothrow) T[MAX];
assert(dati != NULL);
for(int i=0;i<cima;i++)
{
this->dati[i] = p.dati[i]
}
}
template <class T>
bool Pila<T>::push(T x)
{
if(codaPiena())
return false;
dati[cima++] = x;
return true;
}
template <class T>
bool Pila<T>::pop(T& x)
{
if(codaVuota())
return false;
x = dati[--cima];
return true;
}
template <class T>
bool Pila<T>::codaVuota()
{
return (cima == 0);
}
template <class T>
bool Pila<T>::codaPiena()
{
return (cima == MAX);
}
template <class T>
void Pila<T>::reset()
{
cima = 0;
}
codice:
//main.cpp
#include <iostream>
#include "Pila.h"
using namespace std;
int main(){
Pila<int> p1(40);
Pila<int> q;
Pila<char> p2;
p1.push(15);
p2.push('C');
//q = p1;
cin.get();
return 0;
}
Uso Visual Studio 2008 SP1, in ambiente Windows (XP sp3)