Linked List Class with Pointers to Objects and using Polymorphism

This is a discussion on Linked List Class with Pointers to Objects and using Polymorphism within the C++ Programming forums, part of the General Programming Boards category; I am having the most difficult time setting up a linked list class of objects in a heirarchy while incorporating ...

  1. #1
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45

    Linked List Class with Pointers to Objects and using Polymorphism

    I am having the most difficult time setting up a linked list class of objects in a heirarchy while incorporating polymorphism. The below represents a test case, and I couldn't even get this to work! It's the smallest test case I could provide... I apologize for the length but I don't know how to make it shorter. The actual program is ALOT larger.

    Right now, all I would like to do is append to the list, and print the objects AND the number of objects found. Figured it would be simple if I attempted a test case and kept it to three levels- Base, Derived, and Last.. and also the List implementation. I've been on this for three days and I am now a zombie. Where am I going wrong, with the exception of not acquiring more sleep...?

    Code:
    #include <iostream>
    using namespace std;
    
    class Base {
    private:
      string DataInBase;
      Base *Next;
    public:
      Base() {
        cerr << "Base default constructor called." << endl;
      }
      Base(const string& B) 
        : DataInBase( B ) {
          cerr << "Base one param-constructor called." << endl;
      }
      virtual ~Base() {
        cerr << "Base destructor called." << endl;
      }
    
      string getDataInBase() { return DataInBase; }
      Base *getNext() { return Next; }
      void setNext(Base *B) { this->Next = B; }
    
      virtual void output( ostream& os ) {
        os << getDataInBase() << endl;
      }
    };
    
    class Derived : public Base {
    private:
      string DataInDerived;
    public:
      Derived() {
        cerr << "Derived default constructor called." << endl;
      }
      Derived(const string& B, const string& D) 
        : Base( B ), DataInDerived( D ) {
          cerr << "Derived two param-constructor called." << endl;
      }
      virtual ~Derived() {
        cerr << "Derived destructor called." << endl;
      }
    
      string getDataInDerived() { return DataInDerived; }
    
      virtual void output( ostream& os ) {
        os << getDataInDerived() << endl;
      }
    };
    
    class Last : public Derived {
    private:
      string DataInLast;
    public:
      Last() {
        cerr << "Last default constructor called." << endl;
      }
      Last(const string& B, const string& D, const string& S) 
        : Derived( B, D ), DataInLast( S ) {
          cout << "Last three param-constructor called." << endl;
      }
      ~Last() {
        cerr << "Last constructor called." << endl;
      }
      
      string getDataInLast() { return DataInLast; }
    
      void output( ostream& os ) {
        os << getDataInLast() << endl;
      }
    };
    
    class Node {
      friend class List;
    public:
      Node *nextNode;
      Node *baseObject;
    public:
      Node(Node *obj) 
        : nextNode(NULL), baseObject(obj) {
          cerr << "Node added." << endl;
      }
    };
    
    class List {
    private:
      Node *Head;
      Node *tail;
      int size;
    public:
      List() 
        : Head(NULL), size(0) { 
          cerr << "List constructor called." << endl;
      }
      ~List() {
        cerr << "List destructor called." << endl;
      }
      void setListSize( int num ) { this->size = num; }
      int getListSize() { return size; }
      //List *getNext() { return Head; }
      //void setNext( List *L ) { this->Head = L; }
    
      void append(Node *object) {
        Node *temp = new Node(object);
        if (size) {
          temp->baseObject = tail;
          tail->nextNode = temp;
          tail = temp;      
        } else {
          Head = temp;
        }
        size++;
        
        //Node *temp = new Node(object);
    
        //temp = object;
        //temp->setNext(NULL);
        //int currSize = getListSize();
        //setListSize(currSize++);
      }
    };
    
    int main( int argc, char *argv[] ) 
    {
      string First = "FirstParam", Second = "SecondParam", 
              Third = "ThirdParam";
    
      List *listObject; // construct the list with empty params
      
      cout << endl;
      Last Data(First, Second, Third);
      cout << endl;
    
      Derived SecondData("Foo", "Bar");
      cout << endl;
    
      Base *Ba = &Data;
      Ba->output(cout);
    
      // add the objects to the list
      listObject->append(&Ba); 
    
      Ba = &SecondData;
      Ba->output(cout);
    
      listObject->append(&Ba);
    
      // output the size of the list
      cout << "No. of Objects in the List: " << listObject->getListSize() << endl;
    
      return 0;
    }
    Thank you.

  2. #2
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45
    I suppose this is a stupid question?

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    listObject->append(&Ba);
    listObject is not initialized dangling pointer pointing nowhere...
    you cannot use it.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What errors are you getting? Why do you say it is going wrong?

    At quick glance, I'm wondering how you plan to store Base, Derived or Last objects in your List when your List class uses the Node class, but the Node class has no method of storing an object from your class hierarchy. I don't think you need a Node class at all. Just use Base* instead of Node*.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21