Header file Stack.h
#ifndef Stack_h
#define Stack_h
#include <exception>
#include <deque>
using namespace Std;
Class Reademptystackerror:public exception
{
Public
Virtual const char * what () const throw ()
{
Return "Error:stack is empty!!";
}
Private
};
Template<class t>
Class Stack
{
Public
void push (const t& elem)
{
C.push_back (Elem);
}
BOOL Empty () const
{
return C.empty ();
}
T pop ()
{
if (empty ())
{
Throw Reademptystackerror ();
}
T Elem (C.back ());
C.pop_back ();
return elem;
}
t& Top ()
{
if (empty ())
{
Throw Reademptystackerror ();
}
return C.back ();
}
Private
std::d eque<t> C;
};
#endif
Source file Main.cpp:
#include <iostream>
#include "Stack.h"
using namespace Std;
void Main () {
Try
{
Stack<int> s;
S.push (3);
S.push (4);
Cout<<s.pop () <<endl;
Cout<<s.pop () <<endl;
Cout<<s.pop () <<endl;
}
catch (Reademptystackerror e)
{
Cout<<e.what () <<endl;
}
}
Custom exception classes in C + +