Solved

C++ inheritance, virtual, override, syntax

Posted on 2013-11-14
5
370 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

777 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