Add to Favorites    Make Home Page 1842 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Data Structures Home » Stack With OOP and Exception Handling Concept.

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Stack With OOP and Exception Handling Concept.
Author Muhammad Faseeh Abrar From PAKISTAN
Author Email master_02 [at] hotmail.com
Description Stack With OOP and Exception Handling Concept.
Category C++ » Data Structures
Hits 384231
Code Select and Copy the Code
#ifndef STACK_H #define STACK_H template <class A> class Stack { public: virtual void makeNewStack(int) = 0 ; virtual void push(A) = 0 ; virtual A pop() = 0 ; virtual bool isEmpty() = 0 ; virtual bool isFull() = 0 ; virtual A topOfStack() = 0 ; virtual void display() = 0 ; virtual ~Stack(){} virtual A traverse() = 0 ; virtual bool endTrav() = 0 ; virtual void resetTrav() = 0 ; } ; #endif #ifndef STATICSTACK_H #define STATICSTACK_H #include <stdlib.h> #include "Stack.h" #include "StackExceptions.h" #include <iostream> #include<string> using namespace std ; template <class T> class StaticStack : public Stack<T> { private: int top ; int size ; T* array ; int trav; public: StaticStack() { top = -1 ; size = 0 ; array = NULL ; trav = 0 ; } StaticStack(int s) { top = -1 ; size = s ; array = new T[size] ; trav = 0 ; } virtual void makeNewStack(int s) { delete[] array ; top = -1 ; trav = 0 ; size = s ; array = new T[size] ; } virtual void push(T arg) { if(isFull()) { throw StackFull("The Stack is Full") ; } else { if(top == -1) { top = top + 1 ; array[top] = arg; top++; } else { array[top] = arg ; top++ ; } } } virtual T pop() { if(isEmpty()) { throw StackEmpty("The Stack is Empty") ; } else { top-- ; if(top == -1) { throw StackEmpty("The Stack is Empty") ; } else { return array[top] ; } } } virtual T traverse() { /* if( trav == top ) { char ch; cout<<" The value of Traverse reach at it`s Peak"; cout<<" May you Want to Reset The Traverse (y/n) : "; cin>>ch; if(ch=='y') resetTrav();*/ } if(endTrav()) { throw CantTrav("Transverse Cant Proceed"); } else { if( trav > top ) { trav = trav - 2 ; if(trav == -1) throw CantTrav("Transverse Cant Proceed Because The Stack Is Empty"); else { return array[trav++]; } } else if(trav == top) { trav = trav - 2 ; if(trav == -1) throw CantTrav("Transverse Cant Proceed Because The Stack Is Empty"); else { return array[trav]; } } else { if(trav == -1) throw CantTrav("Transverse Cant Proceed Because The Stack Is Empty"); else { return array[trav++]; } } } } virtual bool endTrav() { if(top == -1) { return (trav == top + 1) ; } else return (trav == top) ; } virtual void resetTrav() { trav = 0; } virtual bool isEmpty() { return (top == -1) ; } virtual bool isFull() { return (top == size) ; } virtual T topOfStack() { if(isEmpty()) { throw StackEmpty("The Stack is Empty") ; } else { return (array[top - 1]) ; } } virtual void display() { int i = -1; cout << endl ; for(i = i+1 ; i < top ; i++) { cout << array[i] << ' ' ; } cout << endl ; } virtual ~StaticStack() { delete[] array ; top = 0 ; size = 0 ; array = NULL ; } } ; #endif #ifndef STACKEXCEPTIONS_H #define STACKEXCEPTIONS_H #include <exception> using namespace std ; class StackEmpty : public exception { public: StackEmpty(char* c) : exception(c) {} } ; class StackFull : public exception { public: StackFull(char* c) : exception(c) {} } ; class CantTrav : public exception { public: CantTrav(char* c) : exception(c) {} }; #endif #include "StaticStack.h" //#include "StackExceptions.h" #include <iostream> using namespace std ; void main() { int size; cout<<" Enter the Maximum Size of Stack : "; cin>>size; Stack<int>* S = new StaticStack<int>(size) ; int choice ; int data ; bool cont = true ; while(cont) { try { cout<< endl << "Enter 1 push()" << endl << "Enter 2 to pop()" << endl << "Enter 3 to display()" << endl << "Enter 4 to exit" << endl << "Enter 5 to transverse" << endl << "Enter 6 to reset the transverse" << endl; cin >> choice ; switch(choice) { case 1: cout << endl << "Enter value to push in stack " ; cin >> data ; S->push(data) ; break ; case 2: data = S->pop() ; cout << "The value returned is : " << data ; break ; case 3: S->display() ; break ; case 4: cont = false ; break ; case 5: data = S->traverse(); cout<< "The value returned is : "<<data << endl << endl; break; case 6: S->resetTrav(); break; default: cout <<endl<< "wrong value entered, try again" << endl ; }// switch ends } catch(exception e) { cout << endl << e.what() << endl ; } } // while loop ends delete S ; }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=796


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review