C++ Program to implement Generic classes for maintaining Stack – Q26
Q26. Program to implement Generic classes for maintaining Stack:
Implement Stacks using generic classes with member functions PUSH, POP and DISPLAY. The Program should show implementation of Stack class with at least three different objects.
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <conio.h> #include <stdlib.h> template <class T> class Cstack{ private: T arr[10]; int ptr; public: void init(){ ptr = -1; } void push(T, int); T pop(int &); void clear(); void display(int); }; template <class T> void Cstack<T> :: push(T elm_push, int t_ptr){ if(t_ptr >= 9) cout<<"\n STACK OVERFLOW."; else arr[++t_ptr] = elm_push; } template <class T> T Cstack<T> :: pop(int &t_ptr){ T elm_pop; if(t_ptr <= -1) cout<<"\n STACK UNDERFLOW."; else{ elm_pop = arr[t_ptr--]; cout<<"\n POPPED value is : "<<elm_pop; return elm_pop; } return 0; } template <class T> void Cstack<T> :: clear(){ ptr = -1; cout<<"\n STACK Cleared."; } template <class T> void Cstack<T> :: display(int t_ptr){ cout<<"\n STACK: "; if(t_ptr == -1) cout<<" Empty"; else for(int i=0; i<=t_ptr; i++) cout<<arr[i]<<" "; } int _menu(){ int _ch; cout<<"\n STACK Type:- \n"; cout<<"\n 1 -> Integer."; cout<<"\n 2 -> Float."; cout<<"\n 3 -> Character."; cout<<"\n Enter your choice: "; cin>>_ch; return (_ch); } void main(){ int ch, i_ptr = -1, f_ptr = -1, c_ptr = -1; int i_num; float f_num; char c_num; Cstack<int> Oint; Cstack<float> Ofloat; Cstack<char> Ochar; Oint.init(); while(1){ clrscr(); cout<<"\n STACK - OPERATION"; cout<<"\n ~~~~~~~~~~~~~~~~~ \n"; cout<<"\n 1 -> PUSH"; cout<<"\n 2 -> POP"; cout<<"\n 3 -> CLEAR"; cout<<"\n 4 -> DISPLAY"; cout<<"\n 5 -> EXIT"; cout<<"\n Enter your choice: "; cin>>ch; switch(ch){ case 1: // PUSH ch = _menu(); switch(ch){ case 1: cout<<"\n Enter an Integer Value to push: "; cin>>i_num; Oint.push(i_num, i_ptr); i_ptr++; break; case 2: cout<<"\n Enter a Float Value to push: "; cin>>f_num; Ofloat.push(f_num, f_ptr); f_ptr++; break; case 3: cout<<"\n Enter a Character to push: "; cin>>c_num; Oint.push(c_num, c_ptr); c_ptr++; break; default: cout<<"\n Wrong Choice. No Operation Done."; } break; case 2: // POP ch = _menu(); switch(ch){ case 1: Oint.pop(i_ptr); i_ptr--; break; case 2: Ofloat.pop(f_ptr); f_ptr--; break; case 3: Ochar.pop(c_ptr); c_ptr--; break; default: cout<<"\n Wrong Choice. No Operation Done."; } break; case 3: // CLEAR ch = _menu(); switch(ch){ case 1: Oint.clear(); i_ptr = -1; break; case 2: Ofloat.clear(); c_ptr = -1; break; case 3: Ochar.clear(); c_ptr = -1; break; default: cout<<"\n Wrong Choice. No Operation Done."; } break; case 4: // DISPLAY cout<<"\n\n Integer Stack:-"; Oint.display(i_ptr); cout<<"\n\n Float Stack:-"; Ofloat.display(f_ptr); cout<<"\n\n Character Stack:-"; Ochar.display(c_ptr); break; case 5: // EXIT cout<<"\n\n You chose to EXIT"; getch(); exit(1); default: cout<<"\n\n Enter an appropriate choice."; } // end of switch. getch(); } // end of while. }// end of main.
Output:
STACK – OPERATION
1 -> PUSH
2 -> POP
3 -> CLEAR
4 -> DISPLAY
5 -> EXIT
Enter your choice: 1
STACK Type:-
1 -> Integer.
2 -> Float.
3 -> Character.
Enter your choice: 1
Enter an Integer Value to push: 43
STACK – OPERATION
1 -> PUSH
2 -> POP
3 -> CLEAR
4 -> DISPLAY
5 -> EXIT
Enter your choice: 4
Integer Stack:-
STACK: 43 119 23
Float Stack:-
STACK: 34.549999 78.900002
Character Stack:-
STACK: a n r
Categories: Cpp
Cpp, Generic Classes, Stack Push Pop, Template Classes
Comments (0)
Trackbacks (0)
Leave a comment
Trackback