UP | HOME

C++ Inheritance

Table of Contents

1 Inheritance

It is a mechanizme that allows to create a new classes which extend the existed one.

class Person {
    std::string name_;
    int age_;

public:
    Person () : name_("John"), age_(33) {}
    std::string name() const { return name_; }
    int          age() const { return age_;  }
};

struct Student : Person {
    Student () : uni_("USMA") {}
    std::string university() const { return uni_; }

private:
    std::string uni_;
};

Objects of the derived class can call public methods of the base class



Student s;
std::cout << s.name() << std::endl
          << s.age() << std::endl
          << s.university() << std::endl;

John
33
USMA

There is an object of the base class inside a derived class. When you create an object of a derived class, compiler call the constructor of the base class and then the constructor of a derived class.

class Person {
    std::string name_;
    int age_;

public:
    Person () : name_("John"), age_(33) {}
    Person (const std::string& name, int age) :
        name_(name), age_(age) {}
    std::string name() const { return name_; }
    int          age() const { return age_;  }
};

class Student : public Person {
    std::string uni_;

public:
    Student () : uni_("USMA") {}
    Student (const std::string name, int age, const std::string uni) :
        Person(name, age), uni_(uni) {}
    std::string university() const { return uni_; }
};


Student s("Pavel", 29, "None");
std::cout << s.name() << std::endl
          << s.age() << std::endl
          << s.university() << std::endl;

Pavel
29
None

Now we can use Person object as Student object:

Student s("Pavel", 29, "None");
Person & l = s;                 // Student & -> Person &
Person * r = &s;                // Student * -> Person *
Person p = s;                   // Person("Pavel", 29);

When we assign an object of the derived class to an object of the base class we do copy only these fields that were defined in the base class. (We call construct Person(Person const& p) which doesn't know anything about uni_ field.)

2 Protected

If a structure has access modificator protected it means that

  1. Derived class has no access to private members of the base class.
  2. Derived class has access to a protected members of the base class.
struct Person {
    // 
protected:
    std::string name_;
    int age_;
};

// Can change fields name_ and age_ of the base class Person
struct Student : Person {
    // 
};

3 Access Control and Inheritance

A derived class can access all the non-private members of its base class.

Access public protected private
Same class yes yes yes
Derived class yes yes no
Outside class yes no no

A derived class inherits all base class methods with the following exceptions:

  • Constructors, desctructors and copy constructors of the base class.
  • Overloaded operators of the base class.
  • The friend functions of the base class.

4 Type of Inheritance

When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. protected or private inheritance are hardly used, but public inheritacne is commonly used.

Public inheritance
When deriving a class from a public base class, public members of the base class become public memebers of the derived class and protected members of the base class become protected members of the derived class. A base class private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
Protected inheritance
When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.
Private inheritance
When deriving from a private base class, public and protected members of the base class become private members of the derived class.

Author: Pavel Vavilin

Created: 2018-03-06 Tue 14:59

Validate