In Object Oriented Programming, inheritance is one of the most important principals. It basically means expanding an existing class with methods, fields or perhaps other members. C++ has by far the most flexible inheritance model I’ve ever seen in a programming language. Though it is quite hard to use, compared to Java en C#. For the most part, the syntax is quite similar and if you already understand OOP and inheritance, you’ll get the hang of it in no-time.

Basic extension

public class Dog : public Animal  
{

};

Extending a class is almost the same as in C#, using the colon : operator. Note the ‘public’ in front of the Animal super class. This tells the compiler that the Animal class is visible from the outside. Any methods in the super class can’t be called if you omit the public keyword. It might seem weird, as you probably always want the super class to be visible. This could be useful for wrapper classes.

Overriding methods

In Java, any method is 'virtual’ by default. This means you can override any method in a subclass. In C#, any method is non-virtual by default. If you want to override a method in a subclass, you should add the 'virtual’ keyword to the method in the super class.

In C++ methods are also non-virtual by default. To allow overriding, add the virtual keyword in the header, similar to C#:

public class Animal  
{
public:  
    virtual void MakeSound();
};

public class Dog : public Animal  
{
public:  
    void MakeSound();
};

This is where the confusion starts. In order to override a method, the method must still be declared in the header file of the subclass. You can add the virtual keyword if you want to allow overriding in further subclasses.

Abstract classes

Abstract classes, classes which can’t be used directly, but must be inherited from before an object can be created, work a little differently. You don’t specify the 'abstract’ keyword. To declare a class as abstract, you should make at least one method abstract:

public class Animal  
{
public:  
    virtual void Jump() = 0;
};

Still no 'abstract’ keyword. The above code declares a Jump() method, but no implementation is required in the cpp file. This is because of the ’= 0’, which creates a null pointer for the method. Now, only the header of the method is declared. This class cannot be instanced directly and must first be inherited from. Subclasses must implement the Jump() method:

// Dog.h
public class Dog : public Animal  
{
public:  
    void Jump();
};

// Dog.cpp
void Dog::Jump()  
{
    // Do jump here
}

Now, the Dog class can be instanced and used.

Abstract classes are also a way to define interfaces, as there is no 'interface’ type in C++.

One feature C++ provides over Java-like languages, is 'Multiple Inheritance’. It simply means inheriting multiple classes from a single subclass:

public class Dog : public Animal, public SomeOtherClass  
{

};

Some think this is an incorrect way of OOP, while others think it’s an advantage over Java and C#. In any way, it is a possibility and you can use it if you like.

That’s inheritance in C++, although it’s only the tip of the iceberg. I would like to keep it basic for now and go into deeper subjects in other posts. One of them is generics, which I’ll cover in a next post.