Previous: Classes Up: C++ basics Next: Multiple files

This is an automatically generated documentation by LaTeX2HTML utility. In case of any issue, please, contact us at info@cfdsupport.com.


The C++ inheritance allows to multiple classes share common functionality without replication of the code. For example, a programmer might want to represent a dog in his code. The dog has all properties of an animal as seen in previous example and it has some more on top of that.

Let’s have look at an following example – into the file main.cpp in the directory inheritance in training/cpp_tutorial .

class dog : public animal

    std::string name_;


    // constructor
    // dog has no new data on top of animal
    // so just call the animal constructor
    dog(std::string color, int numberOfLegs, std::string name )
    animal(color, numberOfLegs),

    virtual void bark() const {std::cout << "Bark! Woof! Bark!\n";}

    // get dogs name 
    std::string name() const {return name_;}


The class animal is the same as in the previous example and a new class dog is derived from it. The animal is then called a parent class or a base class, whereas dog is a derived class or a child class. In our case, all objects of type dog are also of type animal, whereas objects of type animal are not necessarily of type dog. The class dog adds new member data representing its name (line 41), a new function to access its name (line 60) and a completely new functionality in the function bark() (line 57).

However, the class hierarchy can be more complicated than just one parent class and one derived class. Multiple classes can be derived from one common base class. See an example in the following excerpt:

class cat : public animal

    // constructor
    // cat has no new data on top of animal
    // so just call the animal constructor
    cat(std::string color, int numberOfLegs)
    animal(color, numberOfLegs)

    void meow() const {std::cout << "Meow! Meow! Meow!\n";}


Moreover, derived classes can serve as a parent class for their own child classes. Example of the class germanShepherd, derived from the class dog follows:

class germanShepherd : public dog
    // constructor
    // germanShepherd has no new data on top of dog 
    // so just call the animal constructor
    germanShepherd(std::string color, int numberOfLegs, std::string name )
    dog(color, numberOfLegs,name)

    virtual void bark() const {std::cout << "Bark! Rghhh!\n";}

Figure: Class hierarchy

One can see visualization of class hierarchy in the figure crossref 3. As one can see, the class germanShepherd has no new member data nor any new functions. Instead, there is again the function bark(). As one might notice, this function is declared as virtual, both in the dog and in the germanShepherd. It means, the function bark() is overridden, i.e. the bark from germanShepherd is going to be used for all the germanShepherd instances. All classes derived in these examples are used in the following simple code:

int main()
    // Max is dog of uncertain breed
    dog max = dog("brown", 4, "Max");

    // Rex is german shepherd
    germanShepherd rex = germanShepherd("black", 4, "Rex");

    // Neighbour has a cat, which lost leg in an car accident
    cat tom = cat("white", 3);

    std::cout << "Our dog " << max.name() << " is " << max.color() << ", has " << max.numberOfLegs() << " legs and barks like this:\n";

    std::cout << "Our german shepard " << rex.name() << " is " << rex.color() << ", has " << rex.numberOfLegs() << " legs and barks like this:\n";

    std::cout << "Neighbours cat has only " << tom.numberOfLegs() << " legs and meows like this:\n";

    return 0;

Let’s compile the program by the following command:

# g++ main.cpp -o animals


# x86_64w64-mingw32-g++ main.cpp -o animals

Running the program should yield following output:

# ./animals
# Our dog Max is brown, has 4 legs and barks like this:
# Bark! Woof! Bark!
# Our german shepard Rex is black, has 4 legs and barks like this:
# Bark! Rghhh!
# Neighbours cat has only 3 legs and meows like this:
# Meow! Meow! Meow!

Now see the difference when the function bark() is called from an object of type dog and germanShepherd.

Previous: Classes Up: C++ basics Next: Multiple files