derived class setup
Mar 27, 2011 at 7:35pm UTC
I am making a queue based on an unordered linked list. I have an unorderedlinkedlist class and a linkedqueue class shown in the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#ifndef H_LINKQUEUE
#define H_LINKQUEUE
#include <assert.h>
#include "queue.h"
#include "unorderedlinkedlist.h"
template <class dType>
struct nodeType
{
dType id;
nodeType<dType> *link;
};
template <class dType>
class linkqueue: public queue<dType>, unorderedlinkedlist<dType>
{
unorderedlinkedlist<dType> _Q;
public :
linkqueue();
bool isEmpty() const ;
bool isFull() const ;
void initqueue();
dType front() const ;
dType back() const ;
void EnQ(const dType& _add);
dType DeQ();
~linkqueue();
};
template <class dType>
bool linkqueue<dType>::isEmpty() const {return (_count == 0);}
template <class dType>
bool linkqueue<dType>::isFull() const {return (_count >= _maxsize);}
template <class dType>
dType linkqueue<dType>::front() const
{
assert(!isEmpty());
}
#endif
as you can see on line 18 i declare an instance of the base class. Is this the correct way to implement it?
Thanks,
-Mike-
Mar 27, 2011 at 9:47pm UTC
Seems redundant to me - as linkqueue is derived from unorderedlinkedlist, so it already contains
an unorderedlinkedlist subobject.
So you have to decide whether:
1. To derive from unorderedlinkedlist and not have the unorderedlinkedlist variable.
2. Not to derive from unorderedlinkedlist and have an unorderedlinkedlist variable.
This depends on the relationship between the two classes.
Mar 28, 2011 at 3:42pm UTC
Ok, I think I would rather not have the variable. To do this do I use the above code, minus line 18, and call function from unorderedlinkedlist class as such
in the EnQ function I could call...
unorderedlinkedlist<dType>::insertlast(dType _add)
Last edited on Mar 28, 2011 at 3:43pm UTC
Mar 28, 2011 at 4:00pm UTC
I understand pretty well about making single classes, and some simple derivation.
Is something more like this what I need?
can anyone give me an example?
1 2 3 4 5
CubeType::CubeType(double h, double l, double w): RectangleType(l, w)
{
height = new double ;
*height = h;
}
Thank you!
Topic archived. No new replies allowed.