C++ Inheritance – Virtual Functions

I have done a fair amount of C++ coding, and it has been my most used language for the past couple of years and I provide Google Helpouts/tutoring on C++. There are several topics which pupils always seem to have trouble with, the hardest one being inheritance.

What is inheritance?

Inheritance in programming is very similar to inheritance in life, you take part of what you have (your genetics) and pass it on to the next generation (your kids). In life it looks like:

  • Grandparent -> parent -> you

In C++ it might look like:

  • Polygon -> Rectangle -> Square

The difference between inheritance in life versus C++, is that in C++ you can pass on everything or create an outline of what you would like (this is called a pure virtual function). This is part of the reason I hate the idea of relating objects in programming to real life, it breaks down.. In real life you cannot inherit everything from your parents, but in C++ you can.

How to inherit – C++

The following is an example program:

The output is:
base
derived

Notice, how class Derived: public Base is used to define class Derived. This means, I inherit all of the public functions from class Base and put them under the public functions in Derived. Further, the “virtual” I used to define the string function in the base class is very important. If we were to remove it the output would become:

base
base

This is because the “virtual” directive lets the compiler know that the Base function is going to be overridden by a new function in the Derived class. This means you can have two functions called the same thing in a Base and Derived class, but provide different functionality depending on which you are calling via the Base pointer.

Pure Virtual Function

A pure virtual function is a function which is defined, but not implemented in the Base class. Take for example the following,

Output:
Woof
Meow

The function sound() in the pet class is defined by: virtual string sound() = 0;, this is the way pure virtual functions must be defined. What pure virtual functions allow/enforce is creating overriding functions in subclasses, and you ensure that any time call p->sound() (if your program compiles) you will get a sound for a particular pet. If for example you did:

p = new Pet();
p->sound();

It would not compile, because p->sound() does not have an implementation.

One Use Case

There are many cases where using a virtual function would be important, but on example I found to be very interesting is counting the number of objects created and destroyed. The following is an example:

Output:

Pets: 4
Cats: 2
Dogs: 1
———-
Cats: 2
Cat Deleted
Cats: 1
Cat Deleted
Dogs: 1
Pets: 2
Dog Deleted
Pets: 1
Abstract Pet Deleted

The code above will add to the count of a specific pet AND abstract pet each time you call the constructor, and remove from both when you call the deconstructor. This works for two reasons (1) we declare four pet object pointers to 2 cats, 1 dog, and 1 abstract pet object (which is used to simply display the “total pet count”), (2) we declared the counting variables static int’s which we initialize to 0 (all objects share this value/memory location).

As we create and destroy objects, the count is update to the shared variable and so every time we call getCount() the object returns the value of what it’s object is pointing to. What is interesting about this, is that we can keep track of both Pet objects and Cat/Dog objects because the Pet constructor/deconstructor is called every time the Cat/Dog object is created/destroyed.

This was a pretty simple example, but I found it interesting and I have noticed that many of the people I tutor have had trouble with this and I hope it helps someone out.

Related Articles:

Helpful Resources

Leave a Reply

Your email address will not be published. Required fields are marked *

 characters available

Time limit is exhausted. Please reload the CAPTCHA.