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.