Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Inheritance in C++

Reference for C++ inheritance: base and derived classes, public/protected/private inheritance, constructor order, name hiding, using declarations and final classes.

Inheritance lets a class (the derived class) reuse and extend another class (the base class). The derived class contains a base-class subobject, gets its accessible members, and can add its own. You declare it with a colon: class Dog : public Animal.

Public inheritance models "is-a": a Dog can be used wherever an Animal is expected. Private and protected inheritance model "implemented in terms of" and are rarely needed; composition is usually cleaner. Inheritance combined with virtual functions gives runtime polymorphism (see Polymorphism).

C++
class Animal { public: void eat(); };
class Dog : public Animal { public: void bark(); };

Try it

C++ Constructor and destructor order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Name hiding, using, and access modes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
class Derived : access Base1, access Base2 { ... };
struct Derived : Base { ... };            // default access: public
class  Derived : Base { ... };            // default access: private
class Leaf final : public Base { ... };   // cannot be derived from
Derived(args) : Base(args), member(x) { }
Inheritance Public base members become Protected base members become
public public protected
protected protected protected
private private private

Private members of the base are never accessible in the derived class, but they are still part of the object.

Returns: no value. A pointer or reference to a derived object converts implicitly to a pointer or reference to an accessible base.

Errors / undefined behavior: deleting a derived object through Base* without a virtual destructor is undefined. Copying a derived object into a base object slices it (Object slicing).

Usage notes

  • Construction order: base classes first (in the order listed), then members, then the derived constructor body. Destruction runs in reverse. Always initialize the base in the initializer list when it has no default constructor.
  • struct vs class: the only difference is default access, and default inheritance access follows it (struct D : B is public, class D : B is private). Write the access explicitly.
  • Name hiding. A member in the derived class hides every base member with the same name, even with different parameters. Use using Base::name; to re-expose the overloads.
  • Protected gives derived classes access to the base's members but not to outsiders, and a derived member function may only access them through its own type. Many designs prefer private data and protected functions.
  • Prefer composition when the relationship is "has-a" or "uses". Inheriting from standard containers (std::vector) is usually a mistake because they have no virtual destructor.
  • Base subobject lifetime. The base part exists while the derived constructor body runs, but the derived part does not yet; virtual calls from constructors resolve to the base version.
  • final on a class (C++11) prevents derivation; on a virtual function it prevents overriding.
  • Inheriting constructors with using Base::Base; forwards them; see Constructors.
  • Empty base optimization: an empty base class takes no space, which is why policy-based design uses inheritance.
  • Multiple inheritance is allowed; see Multiple and virtual inheritance.