2

One of my homework assignments is to create a hierarchy of Stack and Queue. I am required to have a superclass DataStructure which has member functions push and pop. pop is supposed to be declared only once within DataStructure while push is required to be a virtual function. This is what I have so far:

#include <iostream>
#include <vector>

using namespace std;

class DataStructure {
protected:
    vector<int> data;
public:
    void push(int element) { }

    int pop() {
        if (data.size() == 0)
            return -1;

        int elem = data.back();
        data.pop_back();
        return elem;
    }
};


class Stack: public DataStructure {

};


class Queue: public DataStructure {

};

I am stuck. I do not know how to implement the virtual function.

bcr
  • 950
  • 8
  • 28
  • 2
    Check your assignment spec to see if the instructor made mention of pure virtual functions or abstract classes. If they did, you might have to take bcr's answer one step further: `virtual void push(int element) { }` becomes `virtual void push(int element) = 0;` – user4581301 Apr 06 '18 at 04:53

2 Answers2

2

You are very close. Mainly, you are missing the virtual keyword in front of void push().

Explicitly,

void push(int element) { }

becomes:

virtual void push(int element) { }

Or, for a pure virtual function:

virtual void push(int element) = 0;

Furthermore, you can now add push() to both Stack and Queue:

Stack

class Stack: public DataStructure {
public:
    void push(int element) {  
        std::cout << "inside Stack.push()";
    }
}; // Stack

Queue

class Queue: public DataStructure {
public:
    void push(int element) { 
        std::cout << "inside Queue.push()";
    }
}; // Stack


Below is a revision of your code. Keep up the good work!

#include <iostream>
#include <vector>

using namespace std;

class DataStructure {
protected:
    vector<int> data;
public:
    virtual void push(int element) = 0;

    int pop() {
        if (data.size() == 0)
            return -1;

        int elem = data.back();
        data.pop_back();
        return elem;
    } // pop()
}; // DataStructure


class Stack: public DataStructure {
public:
    void push(int element) {  
        std::cout << "inside Stack.push()" << std::endl;
    }
}; // Stack


class Queue: public DataStructure {
public:
    void push(int element) { 
        std::cout << "inside Queue.push()" << std::endl;
    }
}; // Queue
NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
bcr
  • 950
  • 8
  • 28
0

I will use a deque instead of a vector as deque allows insertions and deletions from both ends. Using vector will be inefficient here, as you will need to use insert for inserting at the beginning (for Queue). Here is the quote from cplusplus:

Because vectors use an array as their underlying storage, inserting elements in positions other than the vector end causes the container to relocate all the elements that were after position to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers

NOTE- Please avoid using using namespace std. For details refer -

Here is the full code:

#include <iostream>
#include <deque>

class DataStructure {
protected:
    std::deque<int> data;
public:
    virtual void push(int element) { }

    int pop() {
        if (data.size() == 0)
            return -1;

        int elem = data.back();
        data.pop_back();
        return elem;
    }

    void print() {
        for(std::deque<int>::iterator i=data.begin();i<data.end();i++)
            std::cout << *i;
            std::cout << "\n";
    }
};


class Stack: public DataStructure {
public:
    void push(int element) {  
        data.push_back(element);
    }
}; // Stack


class Queue: public DataStructure {
public:
    void push(int element) { 
        data.push_front(element);
    }
}; // Queue

int main()
{
    Stack s;
    Queue q;
    s.push(1);
    s.print();
    s.push(2);
    s.print();
    s.push(3);
    s.push(4);
    s.print();
    s.push(5);
    s.print();
    s.pop();
    s.print();
    s.pop();
    s.print();
    s.pop();
    s.print();
    s.pop();
    s.print();
    s.pop();
    s.print();

    q.push(1);
    q.print();
    q.push(2);
    q.print();
    q.push(3);
    q.push(4);
    q.print();
    q.push(5);
    q.print();
    q.pop();
    q.print();
    q.pop();
    q.print();
    q.pop();
    q.print();
    q.pop();
    q.print();
    q.pop();
    q.print();

}
Abhishek Keshri
  • 3,074
  • 14
  • 31