1. ## 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.

2. This should clear things up:
Code:
```class Base
{
public:
virtual void display() {cout << "Base" << endl;}
};

class DerivedA : public Base
{
public:
virtual void display() {cout << "DerivedA" << endl;}
};

class DerivedB : public Base
{
public:
virtual void display() {cout << "DerivedB" << endl;}
};

int main(void)
{
vector<Base> v1;
vector<Base*> v2;
int n;

v1.push_back(*(new DerivedB()));
v1.push_back(*(new DerivedB()));
v1.push_back(*(new DerivedA()));
v1.push_back(*(new Base()));

cout << "instance vector:" << endl;
for (n = 0; n < v1.size(); n++)
v1[n].display();

v2.push_back(new DerivedB());
v2.push_back(new DerivedB());
v2.push_back(new DerivedA());
v2.push_back(new Base());

cout << endl << "pointer vector:" << endl;
for (n = 0; n < v2.size(); n++)
v2[n]->display();

return 0;
}```
[EDIT]
Calls to "delete" omitted for brevity

gg

3. zip up your project and ill take a look at it.

4. There's not really much finished yet to the project, it's in its beginning stages. There are two files: a cpp file and a header file, and they're both attached in the zip below.

Here's my response for the program you posted:
instance vector:
Base
Base
Base
Base

pointer vector:
DerivedB
DerivedB
DerivedA
Base
Is there any way to make the instance vector use the most derived function, like the pointer vector does?

5. No, have to use a pointer. Is there a reason why you want to use instances?

gg

6. Just to save on the hassle of memory management with new and delete.

Thanks for the info.

7. ygf what codeplug tried to show you is that if you use objects rather than pointers to objects you in fact slice off the derivedness of the object as so as it is now a base and not a derived it has full base behaviour. Thats why base functions get called. This is one of the most important concepts of polymorphic OOP. Read all you can about slicing. Understand why and how slicing occurs. You virtually never need to slice an object deliberately. And to do so as a mistake leads to obvious problems.