0

Is it possible to derive a child from an array Class?

What I am playing with right now is: Creating an array of Linked Lists

I am building a List class from which I can derive different types of lists (ie. Linear, Circular, Double Linked, etc...

What I would like to do is to extend an array class to make a "arrayOfLists" class. Then I would take the child class and add to it a LinkedList object member.

Is this possible? Am I even thinking of OOP correctly in this instance?

Thank you for your help

5
  • btw - I am working on a class project. I'm not looking for solutions other than being pointed in the right direction. Commented Oct 23, 2013 at 20:18
  • Can you give us at least a pseudo-code example? Commented Oct 23, 2013 at 20:19
  • 2
    What do you mean by "an array class"? You can have an array containing objects of any class that you like, but an array is not a class. Please clarify. Commented Oct 23, 2013 at 20:21
  • 1
    I'm almost positive you can't extend from the array primitive itself i.e. int a[]; However, you can create a wrapper class that holds an array as its core variable and then write functions for your class that help with array operations. In fact, c++11 proposes something like this: cplusplus.com/reference/array/array Commented Oct 23, 2013 at 20:23
  • im not sure why i got downvoted (oh well). (ex). I was thinking to create an array in which each member has a "root" pointer to an unique ll. I thought that rather than creating the code each time I want to do so, that i could use inheritance to simplify the coded acts of growing and shrinking the array for each ll I want to add. Is that more clear? Commented Oct 23, 2013 at 20:59

1 Answer 1

2

The fact that you're talking about it as an arrayOfLists class is a pretty good clue that inheritance is the wrong tool for this job.

Inheritance (public inheritance, anyway) should only be used when the derived class can be substituted for the base class under any possible circumstances. In other words, that an arrayOfLists could be used anywhere a List could be used. Although that might be possible, it seems fairly unlikely.

It sounds to me like what you want is really just an array-like template (e.g., std::vector) instantiated over one of your linked list classes.

Sign up to request clarification or add additional context in comments.

3 Comments

Seeing as this is for a "class project" on OOP, it's probable he's expected to produce something using classic OO concepts - in which case Java or C#'s container classes might be a better guide. C++'s template based containers with no (explicit) parent interface don't really fit into the inheritance heavy OO that's commonly taught.
@JoeGauterin: I can at least hope they won't demand bad design for a class project, can't I?
yes.. im learning OOP. Thank you for being insightful. @Jerry - i was thinking more about extending from some sort of primitive array class (as zero298 commented).. which it seems can't be (nor need be) done. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.