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

Conteúdo do Curso

C++ OOP

C++ OOP

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

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:

1. What is inheritance in object-oriented programming?
2. How is a derived class declared
3. Which classes is suitable to reuse code from the Person class?

What is inheritance in object-oriented programming?

Selecione a resposta correta

How is a derived class declared

Selecione a resposta correta

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

Selecione algumas respostas corretas

Tudo estava claro?

Seção 4. Capítulo 1
We're sorry to hear that something went wrong. What happened?
some-alt