Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

C++ inheritance, virtual, override, syntax

I need an overview of the syntax for doing this in C++. I have a BaseClass, and an ExtendedClass which inherits the BaseClass.

In the BaseClass I define a function foobar. In the ExtendedClass I may or may not want to override that function foobar providing alternate code for it to do.

If my ExtendedClass does not override foobar, then I want a call to foobar to run the BaseClass foobar.

If my ExtendedClass does override foobar, then I want a call to foobar to run the ExtendedClass foobar.

Sounds easy enough. I just need an example, as I get all tangled up in where to put the virtual keyword, and/or the override keyword. ('overrides'? 'overrideable'?)


Currently if I say virtual foobar in the BaseClass.h file, then I have to add virtual foobar in the ExtendedClass.h file if I want to override it, plus add the actual code in the ExtendedClass.cpp file. So I have to change two places, make sure they are in sync, and I don't like that; unless that's the way it's supposed to be for C++. I'm more of a C# programmer but this project requires C++.
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deleyd

ASKER

For ExtendedClass1 what's the difference if I remove the virtual keyword in:
virtual void foobar()

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial