fork download
  1. #include <iostream>
  2. const int SIZE = 10;
  3. template <class T>
  4. class Stack {
  5. public:
  6. Stack() {
  7. printf("%s\n","Init Stack.");
  8. vis = 0;
  9. }
  10. ~Stack() {
  11. printf("\n%s", "Deconstructor:stack is distroyed.");
  12. }
  13. void push(T v);
  14. T pop();
  15. private:
  16. T stack[SIZE];
  17. int vis;
  18. };
  19. template <class T> T Stack<T>::pop() {
  20. if(vis == 0) {
  21. printf("%s\n", "empty stack");
  22. return 0;
  23. }
  24. vis--;
  25. return stack[vis];
  26. }
  27. template <class T>void Stack<T>::push(T v) {
  28. if(vis == SIZE) {
  29. printf("%s\n", "Full Stack");
  30. return;
  31. }
  32. std::cout<<"Added to Stack:"<<v<<"\n";
  33. stack[vis] = v;
  34. vis++;
  35. }
  36.  
  37.  
  38. int main(int argc, char const *argv[]) {
  39.  
  40. Stack<int> st_i;//stack of integers
  41. Stack<float> st_f; //stack of floats
  42. Stack<double> st_d; //stack of doubles
  43. Stack<char> st_c; //stack of characters
  44.  
  45. st_i.push(1);
  46. st_i.push(2);
  47. st_i.push(3);
  48. std::cout<<"Pop: "<<st_i.pop()<<"\n";
  49. std::cout<<"Pop: "<<st_i.pop()<<"\n";
  50. std::cout<<"Pop: "<<st_i.pop()<<"\n";
  51.  
  52. st_d.push(12.3);
  53. st_d.push(16.2);
  54. std::cout<<"Pop: "<<st_d.pop()<<"\n";
  55. std::cout<<"Pop: "<<st_d.pop()<<"\n";
  56. std::cout<<"Pop: "<<st_d.pop()<<"\n";
  57.  
  58. st_c.push('A');
  59. st_c.push('B');
  60. st_c.push('C');
  61. std::cout<<"Pop: "<<st_c.pop()<<"\n";
  62. std::cout<<"Pop: "<<st_c.pop()<<"\n";
  63. std::cout<<"Pop: "<<st_c.pop()<<"\n";
  64.  
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0.01s 5516KB
stdin
Standard input is empty
stdout
Init Stack.
Init Stack.
Init Stack.
Init Stack.
Added to Stack:1
Added to Stack:2
Added to Stack:3
Pop: 3
Pop: 2
Pop: 1
Added to Stack:12.3
Added to Stack:16.2
Pop: 16.2
Pop: 12.3
Pop: empty stack
0
Added to Stack:A
Added to Stack:B
Added to Stack:C
Pop: C
Pop: B
Pop: A

Deconstructor:stack is distroyed.
Deconstructor:stack is distroyed.
Deconstructor:stack is distroyed.
Deconstructor:stack is distroyed.