1

I have a linked list class (List) that deals only with objects of type Node. It can do all sorts of things with these Nodes assuming that they have boolean comparisons overloaded properly. The thing is, I want to use this List class with a special kind of Node called a Term (an algebraic term with a coefficient and an exponent or degree). How do I tell my List class to use the Term functions (Term has special versions of the Print() function and comparison operators) even though it deals with the Terms using Node pointers? For example, my Print() is something like:

Node* walker=head;
while(walker)
{
     walker->Print();
     walker=walker->next;
}

Except there is no Node::Print(), I want it to call Term::Print()! Do I have to make a whole new List class to deal with Term class objects?

1
  • Please add language tag (probably C++) Commented Nov 16, 2012 at 20:37

3 Answers 3

1

This is a classic example of Polymorphism. You can add a function Print() to your Node class just like WhozCraig suggested. (Please read up Virtual Functions and Abstract classes in C++.) You can make Print() a virtual function. You can decide whether you want to make Print() a pure virtual function. If it's a pure virtual function, it will be declared like this in the base class.

class Node{
  virtual void Print() = 0;
  // If you don't want this to be pure virtual 
  // You can give a generic definition
}

In this case, since you did not define Print() in the base class, each derived class which is not meant to be abstract, must implement this method. Thus, the Term class can derive from Node class and implement it's Print() method accordingly :) And you can use the base class pointer to call this function. If in the future you decide to subclass Node and add a different implementation of Print(), you don't have to change Node at all :)

Hope this helps!

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

1 Comment

This did help a lot. But now it gets more complicated, as I want to be able to use boolean comparison operators on Term objects using a Node pointer. For example, if (*nodeptr1 >= *nodeptr2) {foo();} where nodeptr1 and nodeptr2 point to objects of type Term which are derived from the Node class. I've put virtual bool functions in the Node class, and then declared and defined them in the Term class, but it still says the Term functions are abstract. Confusing!
0

You're already diverging from usual list design - would recommend using templates instead of deriving from Node class. You then would want a foreach method which will do an operation on each node, in this case print. Strongly recommend using the C++ standard library containers instead of coding all this "raw".

Another option (less standard and with design flaws) would be to derive out a PrintList that will call the Print function, and it will need to be templated or to be done in terms of Term nodes as the compiler will expect this function.

3 Comments

It's for an assignment where we are to build a linked list class and then use inheritance to adapt the class to different types of nodes (polynomials, lists of names, etc). I'm guessing for each case I should directly modify the node class to include appropriate changes for each use... and I don't yet understand how to do templates, as we haven't yet covered those in the course.
Then I'd head right for them, as they are the appropriate tool for this job.
Agreeing with DeadMG, essentially if you're not going to use templates then you're going to have to do something that's poor design or incorrect or bad practice anyway, so not really much point in avoiding duplicating a class.
0

Three choices:-

In the real world, you would use std::list or a similar container class

Or, you can add Print() as a virtual method to Node, (and make it abstract, potentially)

class Node {
  ...
  virtual void Print() = 0;
}

Or, you can use cast the Node* to a Term*

    Term *t = boost::polymorphic_cast<Term*>(walker);
    t->Print();

Comments

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.