Which class has to be specified to use a virtual function

When setting up classes to use virtual functions, (ie: 3 classes using a print function), is it mandatory to put the base class as a formal parameter or can I use a derived class as a formal parameter ? Thanks
What do you mean by formal parameter?
If I understand correctly what I assume you actually mean - then yes, you can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Base {
public: 
   virtual void f();
};

class Intermediate : public Base {
};

class Derived : public Intermediate {
public:
   virtual void f(); // This will override the virtual function in the Base class.
};
Last edited on
Topic archived. No new replies allowed.