I'm working on exception handling. Here is my code, I need some help..
Requirements:
=> If the user push a value to stack when stack is full,it must throw an exception of type "cInvalidPush".
=> If the user pop a value from stack when stack is empty , it must throw an exception of type "cInvalidPop" .
=> The class "cInvalidPush" is inherited from the class "cInvalidStack" .
=> The class "cInvalidPop"is inherited from the class "cInvalidStack" .
=> In the exception classes mentioned above, their objects must be able to store error message in them.
=> Create an object of CStack in main and show how you would catch the exceptions if any of above occurs.
class that holds the prototype of all functions:
#include<iostream>
#include<stdexcept>
using namespace std;
class CStack //holds the prototype of functions
{
private:
int size; //holds size of stack
int top; //contains the index value of array from where the value will be pushed and poped
int *ptr_stk; //points to an array of integer
int isFull() const; //returns 1 if stack is empty otherwise 0
int isEmpty() const; //returns 1 if stack is full otherwise 0
public:
CStack(int=10); //default size for the stack is 10
~CStack();
void push(const int); //push the value in array of integers pointed by int *ptr_stk
int pop(); //pop the value array of integers pointed by int *ptr_stk from the top of stack
}; //end class CStack
and now here is the defintion of all prototypes:
int CStack::isEmpty() const
{
if(top==0)
return 1;
else
return 0;
}
int CStack ::isFull() const
{
if (top==size)
return 1;
else
return 0;
}
CStack::CStack(int stackSize)
{
size=stackSize;
ptr_stk=new int[size]; //allocates the pointer-based space
for(int i=1;i<=size;i++)
ptr_stk[i]=0;
}
void CStack::~CStack()
{
delete [] ptr_stk; //de-allocates the space
}
void CStack::push(const int val)
{
int decision1;
decision1=isFull();
if(decision1==1)
throw cInvalidPush;
else
for(int j=1;j<=top;j++)
{
ptr_stk[i]=val;
}
}
void CStack::pop()
{
int decision2;
decision2=isEmpty();
if(decision2==1)
throw cInvalidPop;
else
delete [] ptr_stk;
}
I made two classes for exception:
class cInvalidPush:public cInvalidStack //exception if stack is full
{
public:
cInvalidPush():cInvalidStack("Stack if full.")
{
}
}; //end class
class cInvalidPop:public cInvalidStack //exception if stack is empty
{
public:
cInvalidPop():cInvalidStack("Stack is empty.")
{
}
}; //end class
but i don't know what to write in class cInvalidStack? it was mentioned in the question that these two classes are inherited from class cInvalidStack...
My second question is can anyone tell me what to write in it's main() ?
because I'm throwing two exceptions in my program. Rather one of them occurs at a time. So how to handle two exceptions in one catch? and how to make its TRY block?
Oh, i tried and here it is:
int main()
{
CStack obj; //instantiating object
int value;
while(cin>>value)
{
try
{
cout<<"enter the value in stack: "<<endl;
obj.push(value);
}
catch
{
/*no idea?
*/
}
}
return 0;
}
Can anyone complete it please? and one other thing how to handle the data member top ?