Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ inheritance, virtual, override, syntax

Posted on 2013-11-14
5
Medium Priority
?
378 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 1500 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 1500 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 1500 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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 clear a vector as well as how to detect empty vectors in C++.

688 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