Зміст курсу
C++ OOP
C++ OOP
Introduction to Inheritance
Inheritance is a fundamental concept in object-oriented programming that allows a new class
to inherit properties and behaviors from an existing one. Through inheritance, the new class
, often referred to as a subclass, gains the attributes and methods of the existing class, known as the superclass.
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
A 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_inheritance
class Base { // Base class members }; class Derived : public Base { // Derived class members };
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 theaccessSpecifier
andBaseClass
indicates thatDerivedClass
is inheriting fromBaseClass
.
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:
Дякуємо за ваш відгук!