Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Introduction to Inheritance | Inheritance Overview
C++ OOP
course content

Зміст курсу

C++ OOP

C++ OOP

1. Fundamentals of OOP
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

bookIntroduction to Inheritance

Why do We Need Inheritance

To better understand the concepts of inheritance, let’s look at some practical examples. The primary benefit of inheritance is code reuse. For example, Student and Teacher classes reuse the code from Person (name, age, and display_info method). This reduces redundancy and makes the code more maintainable.

Syntax of inheritance

Derived class is declared using a class declaration that specifies the base class from which it inherits. This is done using a colon followed by the access specifier (public, protected, or private) and the base class name.

  • Base Class (superclass): the class whose properties and functions are inherited. It is also known as the parent or superclass.
  • Derived Class (subclass): the class that inherits from the parent. It is also known as the child or subclass.
  • Access Specifier: this specifier determines how the members of the base class are inherited by the derived class.
  • Inheritance: the colon (:) followed by the accessSpecifier and BaseClass indicates that DerivedClass is inheriting from BaseClass.

Note

The role and impact of access specifiers in inheritance will be covered in a later section of this chapter

Types of Inheritance

There are multiple times of inheritance. Each type offers a unique way of establishing relationships between classes, thereby providing a foundation for effective object-oriented design. Here are the main types of inheritance:

Single InheritanceInvolves one base class and one derived class.
Multiple InheritanceA derived class inherits from more than one base class.
Multilevel InheritanceInvolves a chain of classes where a class is derived from another derived class.
Hierarchical InheritanceMultiple classes are derived from a single base class.
Hybrid InheritanceA combination of two or more types of inheritance.
Which classes is suitable to reuse code from the Person class?

Which classes is suitable to reuse code from the Person class?

Виберіть кілька правильних відповідей

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 1
some-alt