Advertisement
Guest User

pointers c++

a guest
Nov 14th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Car {
  6. public:
  7. virtual ~Car() { cout << "we delete Car"; };
  8.  
  9. public:
  10. virtual double speed() { return 14; };
  11. };
  12.  
  13.  
  14. class Dacia : public Car {
  15. public:
  16. ~Dacia() { cout << "we delete Dacia"; };
  17.  
  18. public:
  19. double speed() { return 15; };
  20.  
  21. };
  22.  
  23. class StackOverflowException : exception {
  24. };
  25.  
  26. class StackUnderflowException : exception {
  27. };
  28.  
  29. template <class T>
  30. struct StackBox{
  31. StackBox *mNextOnStack;
  32. T mValue;
  33. };
  34.  
  35. template <class T>
  36. class MyStack{
  37. public:
  38. MyStack(): mStackTop(NULL){};
  39. public:
  40. void push(T);
  41. T pop(void);
  42.  
  43. private:
  44. StackBox<T> *mStackTop;
  45. };
  46.  
  47.  
  48. template <class T>
  49. void MyStack<T>::push(T value){
  50. StackBox<T> *sb = new StackBox<T>();
  51. if(sb == NULL)
  52. throw new StackOverflowException();
  53.  
  54. sb->mValue = value;
  55. sb->mNextOnStack = mStackTop;
  56. mStackTop = sb;
  57. }
  58.  
  59. template <class T>
  60. T MyStack<T>::pop(){
  61. if(mStackTop!=NULL){
  62. T v = mStackTop->mValue;
  63. StackBox<T> *nos = mStackTop->mNextOnStack;
  64. delete mStackTop;
  65. mStackTop = nos;
  66. return v;
  67. }
  68. else {
  69. throw new StackUnderflowException();
  70. }
  71. }
  72.  
  73. int main()
  74. {
  75. cout<<"Hello World"<<" ";
  76. double a=45, b=78.67, c=90;
  77. MyStack<double> numere;
  78.  
  79. numere.push(a);
  80. numere.push(b);
  81. numere.push(c);
  82.  
  83. cout<<numere.pop()<<" ";
  84. cout<<numere.pop();
  85.  
  86. Dacia *myDacia = new Dacia();
  87.  
  88. Car *pMyCar;
  89. pMyCar = myDacia;
  90.  
  91.  
  92. cout << "speed " << pMyCar->speed();
  93.  
  94. delete pMyCar;
  95.  
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement