Exception Handling

 


Exception Handling:

      Exception handling in C++ allows you to gracefully handle errors or exceptional situations that may occur during program execution. It helps you write robust and reliable code by providing a mechanism to catch and handle exceptions

    When a problem or an error occurs within a function, you can use the throw keyword to create an exception object and propagate it up the call stack. To handle exceptions, you use try and catch blocks. A try block encloses the code that may throw an exception, while a catch block handles the exception.

 

Code#1:

Program to test the various operations of a stack:
#include <iostream>
#include "myStack.h"
using namespace std;
void testCopyConstructor(stackType<int>otherStack);
int main()
{
stackType<int> stack(50);
stackType<int>copyStack(50);
stackType<int>dummyStack(100);
stack.initializeStack();
stack.push(23);
stack.push(45);
stack.push(38);
copyStack = stack; //copy stack into copyStack
cout<< "The elements of copyStack: ";
while (!copyStack.isEmptyStack()) //print copyStack
{
cout<<copyStack.top() << " ";
copyStack.pop();
}c
out <<endl;
copyStack = stack;
testCopyConstructor(stack); //test the copy constructor
if (!stack.isEmptyStack())
cout<< "The original stack is not empty." <<endl
<< "The top element of the original stack: "
<<copyStack.top() <<endl;
dummyStack = stack; //copy stack into dummyStack
cout<< "The elements of dummyStack: ";

 

while (!dummyStack.isEmptyStack()) //print dummyStack
{
cout<<dummyStack.top() << " ";
dummyStack.pop();
}c
out <<endl;
return 0;
}
void testCopyConstructor(stackType<int>otherStack)
{
if (!otherStack.isEmptyStack())
cout<< "otherStack is not empty." <<endl
<< "The top element of otherStack: "
<<otherStack.top() <<endl;
}
Code#1(part 1):

myStack.h

#ifndef H_StackType

#define H_StackType

#include <iostream>

#include <cassert>

#include "stackADT.h"

using namespace std;

template <class Type>

class stackType: public stackADT<Type>

{

public:

const stackType<Type>& operator=(const stackType<Type>&);

void initializeStack();

bool isEmptyStack() const;

bool isFullStack() const;

void push(const Type&newItem);

Type top() const;

void pop();

stackType(int stackSize = 100);

stackType(const stackType<Type>&otherStack);

~stackType();

private:

int maxStackSize; //variable to store the maximum stack size

int stackTop; //variable to point to the top of the stack

Type *list; //pointer to the array that holds the

//stack elements

void copyStack(const stackType<Type>&otherStack);

};

template <class Type>

void stackType<Type>::initializeStack()

{

stackTop = 0;

}//end initializeStack

template <class Type>

bool stackType<Type>::isEmptyStack() const

{

return (stackTop == 0);

}//end isEmptyStack

template <class Type>

bool stackType<Type>::isFullStack() const

{

return (stackTop == maxStackSize);

} //end isFullStack

template <class Type>

void stackType<Type>::push(const Type&newItem)

{

if (!isFullStack())

{

list[stackTop] = newItem; //add newItem to the

//top of the stack

stackTop++; //increment stackTop

}

else

cout<< "Cannot add to a full stack." <<endl;

}//end push

template <class Type>

Type stackType<Type>::top() const

{

assert(stackTop != 0); //if stack is empty,

//terminate the program

return list[stackTop - 1]; //return the element of the

//stack indicated by

//stackTop - 1

}//end top

template <class Type>

void stackType<Type>::pop()

{

if (!isEmptyStack())

stackTop--; //decrement stackTop

else

cout<< "Cannot remove from an empty stack." <<endl;

}//end pop

template <class Type>

stackType<Type>::stackType(int stackSize)

{

if (stackSize<= 0)

{

cout<< "Size of the array to hold the stack must "

<< "be positive." <<endl;

cout<< "Creating an array of size 100." <<endl;

maxStackSize = 100;

}

else

maxStackSize = stackSize; //set the stack size to

//the value specified by

//the parameter stackSize

stackTop = 0; //set stackTop to 0

list = new Type[maxStackSize]; //create the array to

//hold the stack elements

}//end constructor

template <class Type>

stackType<Type>::~stackType() //destructor

{

delete [] list; //deallocate the memory occupied

//by the array

}//end destructor

template <class Type>

void stackType<Type>::copyStack(const stackType<Type>&otherStack)

{

delete [] list;

maxStackSize = otherStack.maxStackSize;

stackTop = otherStack.stackTop;

list = new Type[maxStackSize];

//copy otherStack into this stack

for (int j = 0; j <stackTop; j++)

list[j] = otherStack.list[j];

} //end copyStack

template <class Type>

stackType<Type>::stackType(const stackType<Type>&otherStack)

{

list = NULL;

copyStack(otherStack);

}//end copy constructor

template <class Type>

const stackType<Type>&stackType<Type>::operator=

(conststackType<Type>&otherStack)

{

if (this != &otherStack) //avoid self-copy

copyStack(otherStack);

return *this;

} //end operator=

#endif

stackADT.h:

 

#ifndef STACKADT_H

#define STACKADT_H

 

template <class Type>

class stackADT

{

public:

    // Constructor

    virtual ~stackADT() {}

 

    // Function to initialize the stack

    virtual void initializeStack() = 0;

 

    // Function to check if the stack is empty

    virtual bool isEmptyStack() const = 0;

 

    // Function to check if the stack is full

    virtual bool isFullStack() const = 0;

 

    // Function to push an item onto the stack

    virtual void push(const Type&newItem) = 0;

 

    // Function to pop an item from the stack

    virtual void pop() = 0;

 

    // Function to return the top element of the stack

    virtual Type top() const = 0;

};

 

#endif

Output:





Share this:

ABOUT THE AUTHOR

Hello We are OddThemes, Our name came from the fact that we are UNIQUE. We specialize in designing premium looking fully customizable highly responsive blogger templates. We at OddThemes do carry a philosophy that: Nothing Is Impossible

0 comments:

Post a Comment