Solved

C++ inheritance, virtual, override, syntax

Posted on 2013-11-14
5
372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…

739 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