Virtual functions problem
I'm writing an equation parser, and taking quite a bit of time to do it. Basically, there are three base classes: Expression, Term, Factor. Expression has the ability to contain any number of Term's and Term has the ability to contain any number of Factor's (through vector classes).
To simplify future matters, I decided to derive a few classes from base class Factor: Numeral, Variable, Function, Factor_Expression
class Numeral also derives two other classes from it: Integer, Float
I want to write a display() function which would take as a parameter an ostream& and output some useful data to it. For instance:
Code:
int Integer::display(ostream& out) {
out << value; //value is an int
}
To simplify things again, I decided to put a display() function in Expression, and in Term. ie:
Code:
class Term {
//stuff
vector<Factor> factors;
int display(ostream&);
//other stuff
}
class Expression {
//stuff
vector<Term> terms;
int display(ostream&);
//other stuff
}
The display functions listed here would just call the display functions of each term or factor in the array.
My question is how do I make it so that whenever display() is called for each vector<Factor>, it calls the function of the most derived class? I've declared every display function virtual, but it always calls the Factor base class's function instead.