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

Contenido del Curso

C++ OOP

C++ OOP

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

book
Introduction 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:

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

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

Selecciona unas respuestas correctas

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 1
We're sorry to hear that something went wrong. What happened?
some-alt