Thread: Inheritance with a linked list

  1. #1
    myramains
    Guest

    Question Inheritance with a linked list

    Hello All

    I'm new to C++ so I was hopping you could help me out. I'm trying to do
    a double linked list and I'm not sure what to do when I inherit from the
    linked item. Given the following:

    class cBox
    {
    cBox *Next, *Prev; // Linked list

    public:
    cBox();
    ~cBox()
    };

    class cShoeBox : public cBox
    {

    public:
    cShoeBox();
    ~cShoeBox()
    }


    In cShoeBox can I use Next and Prev from the ancestor and assign a
    cShoeBox to them or do I have to redefine Next and Prev as cShoeBox? If
    I have to redefine them can I still call them Next and Prev or do I have
    to change their names as well to say Next1 and Prev1? No matter what the
    answer is I want to understand why. Please give me as much detail as you
    can. Thank you!

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    Ok.. considering that u r using "public" inheritance.........key word "public" .......

    Now, i see you have few questions, so I'll try to give you as much info as i can and I'll try not to confuse you....


    Code:
    class cBox
    {
    cBox *Next, *Prev; // Linked list
    
    public:
    cBox();
    ~cBox()
    };
    
    class cShoeBox : public cBox
    {
    
    public:
    cShoeBox();
    ~cShoeBox()
    }

    In this example, cShoeBox "inherits" all the PUBLIC assets of the original cBox class, not private but public, there also can be PROTECTED data members of a class however I won't get into this now.
    So keeping in mind that all you inherit comes from PUBLIC only, looking at your sample code, the 2 data members are PRIVATE

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    sorry didn't finish..........(pressed the wrong key)

    continuation of last sentence...

    ....by defualt.
    That means that they are not inhereted through "public" inheritance.
    So, for the derived class cShoeBox, they are not visible nor they exist.

    Trying to answer your question about about re-defining them in the derived class: As long as you would declare these 2 variable in the base class to PUBLIC not private, as you did by default, they would be inherited and visible to the derived class.
    So the operation like:

    Code:
        cShoeBox SomeBox;
        SomeBox->Next;         // dereffrencing a pointer (*Next)
    such operation usually would work fine, however I don't think this is what you're really looking for.

    To answer your question if you can use a pointer from the original class....... yes.....
    It all depends also on your implementation of each class, for example you can access (*Next) directly as an entity (single item belonging to the class itself rather than to an object of it) by:

    cBox::*Next;

    but once again it depends on a lot of things but i don't think i have time to explain all of them.

    To perfectly honest, to be able to answer all of your questions I would have to see all of the code.

    To answer your question about redefining the names: well it also depends on you entire code. From what I'm seeing you are trying to create another node of the linked list, which most likely would be separate from the other nodes, so keeping in mind that by typing:

    cBox *Next;

    you are declaring "Next" to a pointer to an object of a "cBox" class, so even if you would put these 2 pointers as PUBLIC an in turn be able to INHERIT them publicly, I don't think this would really be what you are looking for, b/c *Next still would point to cBox object rather than a "cShoeBox" object which I think is really what you are looking for.....

    well, let me end here, and leave you with couple of these ideas, once again I emphasize the idea of PUBLIC in public inheritance.


    Ok.... I hope this gives you a better idea on this idea of inheritance, to be honest I kind of see where you are going however I think I would have to work with you more on this for you to understand all the aspects of this....

    if you want you can simply e-mail me at:

    [email protected]

    that would probably be the best, considering that those posts would get really long....

    anyway, i hope this helps


    ..matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  5. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM