Skip to content

Chapter 2 of 8

Inheritance and Polymorphism

Inheritance allows a derived class to inherit members from a base class, declared with a colon and access specifier in the derived class header. It promotes code reuse and expresses an is-a relationship between types. C++ supports multiple inheritance, where a class can derive from several base classes at once, and it provides override and final specifiers from C++11: override asserts that a method overrides a base virtual function and produces a compile error if it does not, while final prevents further overriding in derived classes, or further derivation when applied to a class. When a derived class declares a name that matches one in the base, the base name is hidden in the derived class even when signatures differ, a phenomenon called name hiding; a using Base::name declaration in the derived class can unhidde overloaded base functions. The std namespace holds the entire C++ Standard Library, so identifiers like std::cout and std::vector are qualified by it, while user code can group its own identifiers into namespaces to avoid name collisions and bring them into scope with using namespace.

Polymorphism in C++ comes in two forms. Compile-time, or static, polymorphism relies on function overloading, which allows multiple functions in the same scope to share a name as long as their parameter lists differ, on operator overloading, and on templates to choose the right operation at compile time. A binary operator may be overloaded as a member of the left operand's class, where the left operand becomes *this, or as a non-member function, often a friend, taking both operands explicitly; the stream insertion operator is the canonical non-member case so the left operand can be a stream. Runtime, or dynamic, polymorphism uses virtual functions and inheritance, dispatching through a per-class table of function pointers called a vtable. Each object of a class with virtual functions carries a hidden vptr set by every constructor that points to its class's vtable; calling a virtual function through a base pointer or reference fetches the function pointer from the vtable at runtime and invokes the most-derived override. A pure virtual function, declared with the = 0 suffix, has no implementation in the base class, and any class that contains one becomes an abstract class that cannot be instantiated directly.

Abstract classes serve as interfaces or partial blueprints. C++ has no interface keyword, but the convention is a class containing only pure virtual functions and no data members, which derived classes must fully implement to be instantiable as concrete classes. When a derived class inherits from a base that holds resources, declaring a virtual destructor in the base ensures that deleting the object through a base pointer invokes the derived destructor first, releasing derived-class resources. Multiple inheritance introduces the diamond problem: if two bases share a common ancestor, the most-derived class would normally hold two copies of that ancestor. Virtual inheritance, declared with the virtual keyword on the base specifier, guarantees a single shared base subobject and resolves the ambiguity. Operators that cannot be overloaded in C++ are the scope resolution operator, member access, member access through pointer, and the ternary; you also cannot invent new operator symbols.

All chapters
  1. 1Object-Oriented Foundations in C++
  2. 2Inheritance and Polymorphism
  3. 3Object Lifecycle and Resource Management
  4. 4Templates and Generic Programming
  5. 5Standard Library Containers and Iterators
  6. 6Modern C++ Utilities and Language Features
  7. 7Memory Management, Pointers, and Casting
  8. 8Error Handling, Concurrency, and the Compilation Model

Drill it

Reading is not remembering. These come from the Cpp Programming deck:

Q

What are the four main principles of OOP in C++?

The four pillars of OOP are:Encapsulation – bundling data and methods togetherAbstraction – hiding complex implementation detailsInheritance – deriving new clas...

Q

What is a class in C++?

A class is a user-defined type that encapsulates data members and member functions. Defined with the class keyword:class MyClass { public: int x; void show(); }...

Q

What is the difference between a class and a struct in C++?

The only default difference is access control:class members are private by defaultstruct members are public by defaultBoth can have methods, constructors, and i...

Q

What is inheritance in C++?

Inheritance allows a derived class to inherit members from a base class:class Dog : public Animal { };This promotes code reuse and establishes an is-a relations...