Solved

C++ inheritance, virtual, override, syntax

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now