I am confused about the bolded part.
Why do we have to throw Overflow(), Underflow() and Bad_size() and not just throw Overflow, Underflow... without the parantheses - why do we have to throw a "function" and not the type of the object itself?
And same goes for structures, not just classes.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
class Stack {
char *v;
int top;
int max_size;
public:
class Overflow { };
class Underflow { };
class Bad_Size { };
struct ov {
};
Stack(int s);
~Stack();
void push(char c);
char pop();
};
Stack :: Stack(int s)
{
top = 0;
if(s > 10000)
throw Bad_Size();
max_size = s;
v = new char[s];
}
Stack :: ~Stack()
{
delete [] v;
}
void Stack :: push(char c)
{
if(top == max_size - 1)
throw Overflow();
v[top++] = c;
}
char Stack :: pop()
{
if(top == 0)
throw Underflow();
return v[--top];
}
int main(int argc, char *argv[])
{
Stack :: Bad_Size();
try {
Stack sp(10000);
int i;
for(i = 0; i <= 5; i++)
sp.push('c');
}
catch(Stack :: Underflow) {
cout << "underflow!" << '\n';
}
catch(Stack :: Overflow) {
cout << "overflow!" << '\n';
}
catch(Stack :: Bad_Size) {
cout << "bad size!" << '\n';
}
Stack :: Bad_Size();
getchar();
return 0;
}