Solved

C++ inheritance, virtual, override, syntax

Posted on 2013-11-14
5
371 Views
Last Modified: 2013-11-14
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++.
0
Comment
Question by:deleyd
  • 3
5 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 375 total points
ID: 39648775
'virtual' provides the functionality that you are asking for. I.e.

#include <iostream>

using namespace std;

class BaseClass {

public:

  virtual void foobar() { cout << "BaseClass::foobar()" << endl;}
};

class ExtendedClass1 : public BaseClass {

public:

  virtual void foobar() { cout << "ExtendedClass1::foobar()" << endl;}
};

class ExtendedClass2 : public BaseClass {

public:

  // no 'foobar()'
};

int main () {

  BaseClass b;
  ExtendedClass1 e1;
  ExtendedClass2 e2;

  b.foobar();
  e1.foobar();
  e2.foobar();

  return 0;
}

Open in new window



Output:

BaseClass::foobar()
ExtendedClass1::foobar()
BaseClass::foobar()

Open in new window

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 375 total points
ID: 39648785
BTW, there is no 'override' keyword in C++. For more an 'virtual' and inheritance, how it works and when to use it, see e.g. these articles here:

http://www.exforsys.com/tutorials/c-plus-plus/c-virtual-functions.html ("C++ Virtual Functions")
http://www.codersource.net/2010/02/11/c-virtual-function/ ("C++ Virtual Function")
0
 

Author Comment

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

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 375 total points
ID: 39649430
Well, as the articles state:
The main difference between a non-virtual C++ member function and a virtual member function is in the way they are both resolved. A non-virtual C++ member function is resolved during compile time or static binding. Virtual Functions are resolved during run-time or dynamic binding.

In fact, you can achieve the same by omitting 'virtual' - yet it offers a variety of possibilites that are hard to illustrate with such a simple example. Read the article(s) ;o)
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 125 total points
ID: 39649501
Looks like override keyword (but not a reserved word) has been added to C++11 just to help prevent programming errors. If you don't make programming errors, you don't need it.
http://en.cppreference.com/w/cpp/language/override
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert MFC APP to Win32 APP. 19 97
Unable to start eclipse ? 17 154
Outlook 13 43
How do I get Window Title of all opened process? 4 14
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question