DataStructure Class Inheritance
DataStructure Class Inheritance
I am new to C++. I'm reading a C++ book, and I am attempting to solve this exercise. I have created a base class called "DataStructure" that is inherited by a "myArray" class.
The "myArray" class is inherited by two classes: "Stack" and "Queue".
Functions insertA, deleteA, pop and push are declared in the DataStructure class.
I am trying to use inheritance to implement the functionalities of the stack using the myArray class.
I am unsure how to do this. I have attempted to include a pop() function to facilitate this. The error is that top is not a pointer (the error occurs in Stack.cpp).
Any advice on how to solve this would be appreciated.
This is my implementation of the myArray class:
myArray.h
#include "DataStructure.h"
class myArray : public DataStructure
public:
myArray(int = 10); // default constructor (array size 10)
~myArray() delete arrayPtr; // destructor
virtual bool insertA(const int&); // insert an element into the array
virtual bool deleteA(int&); // remove an element from the array
virtual bool isEmpty() return top == -1;
virtual bool isFull() return top == size - 1;
int size; // # of elements in the array`
int top; // location of the top element`
int *arrayPtr; // pointer to the array`
;
myArray.cpp
myArray::myArray(int s) : DataStructure()
size = s > 0 ? s : 10;
top = -1; // array is initially empty
arrayPtr = new int[size]; // allocate space for elements
// Insert an element into the array
// return 1 if successful, false otherwise
bool myArray::insertA(const int &insertValue)
if (!isFull())
arrayPtr[++top] = insertValue; // place item in array
return true; // insert successful
return false; // insert unsuccessful
bool myArray::deleteA(int &insertValue)
if (!isEmpty())
insertValue = arrayPtr[top--]; // remove item from array
return true; // delete successful
return false; // delete unsuccessful
Stack.cpp
Stack::Stack(int s)
: myArray()
void Stack::pop()
int retval = arrayPtr[top];
delete(top);
top
int
size
top
arrayPtr
public
myArrary
Your code is a bit strange. It seems like your prototypes want you to have an
array of stacks
. But based off your inheritance chain you want a stack of arrays
. Which is it?– Callat
Aug 23 at 19:52
array of stacks
stack of arrays
To clarify @WhozCraig 's point...
delete
is only a valid operation on a pointer. (Maybe you meant deleteA
?) Also he is very correct in stating that size
, top
, and arrayPtr
should not be public. I don't think its an overstatement to say that any implementation that requires otherwise is a mess--although a client might be interested in a (effectively) read-only function that returns the size.– zzxyz
Aug 23 at 20:04
delete
deleteA
size
top
arrayPtr
Also, if you are using modern C++, and your various virtual functions are declared in
DataStructure
, I would presume your intention in myArray is to override them, in which case you should use the override
keyword instead of virtual
. What you're doing can work (for backward compatibility reasons), but it can also result in hard-to-detect unexpected behavior with very small mistakes.– zzxyz
Aug 23 at 20:49
DataStructure
override
virtual
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Indeed,
top
is not a pointer; it's simply anint
(the use of which as an array index being demonstrated on the very line prior to the one flagging the error) . The error message should be self-explanatory. None ofsize
,top
, orarrayPtr
should even bepublic
in this exercise. You should be using methods frommyArrary
to implement your stack derivation. They should be sufficient (if coded properly). Finally, don' get bit by failing to comply with the rule of three/five/zero.– WhozCraig
Aug 23 at 19:51