Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Car {
- public:
- virtual ~Car() { cout << "we delete Car"; };
- public:
- virtual double speed() { return 14; };
- };
- class Dacia : public Car {
- public:
- ~Dacia() { cout << "we delete Dacia"; };
- public:
- double speed() { return 15; };
- };
- class StackOverflowException : exception {
- };
- class StackUnderflowException : exception {
- };
- template <class T>
- struct StackBox{
- StackBox *mNextOnStack;
- T mValue;
- };
- template <class T>
- class MyStack{
- public:
- MyStack(): mStackTop(NULL){};
- public:
- void push(T);
- T pop(void);
- private:
- StackBox<T> *mStackTop;
- };
- template <class T>
- void MyStack<T>::push(T value){
- StackBox<T> *sb = new StackBox<T>();
- if(sb == NULL)
- throw new StackOverflowException();
- sb->mValue = value;
- sb->mNextOnStack = mStackTop;
- mStackTop = sb;
- }
- template <class T>
- T MyStack<T>::pop(){
- if(mStackTop!=NULL){
- T v = mStackTop->mValue;
- StackBox<T> *nos = mStackTop->mNextOnStack;
- delete mStackTop;
- mStackTop = nos;
- return v;
- }
- else {
- throw new StackUnderflowException();
- }
- }
- int main()
- {
- cout<<"Hello World"<<" ";
- double a=45, b=78.67, c=90;
- MyStack<double> numere;
- numere.push(a);
- numere.push(b);
- numere.push(c);
- cout<<numere.pop()<<" ";
- cout<<numere.pop();
- Dacia *myDacia = new Dacia();
- Car *pMyCar;
- pMyCar = myDacia;
- cout << "speed " << pMyCar->speed();
- delete pMyCar;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement