Last Updated:

Inheritance in C++

One of the main features of OOP is the ability to inherit. Inheritance is a way of reusing software in which new derived classes (inheritors) are created from existing base classes (parents).

When you create a new class, you inherit from the members and methods of the previously defined base class. The class created by inheritance is a derived class, which in turn can act as a base class for the generated classes. If the method names of the derived and base classes are the same, the methods of the derived class overload the methods of the base class.

When using inheritance, members and methods in addition to the public and private properties can have a protected property. For a single class, the protected and private specifiers are equivalent. The difference between protected and private is evident in inheritance. Private members and methods declared as protected in the base class can be used as public in the derived classProtected members and methods are an intermediate option between public and private.

When you create a derived class, use the following syntax:

1
2
3
4
5
6
7
8
9
10
11
12
class name_derived_class:
type_inheritance base_class
{
private methods and members of the ...

public:
public methods and class
members ...
protected:
protected methods and class members
...

};

Here, name_derived_class is the name of the derived class to be created, type_inheritance is the method of inheritance, the following methods of inheritance are possible: publicprivate, and protectedbase_class is the name of the base type. You should distinguish between the type of access to items in the base class and the type of inheritance.

Inheritance types and access types

Access MethodSpecifier in the base classAccess in a derived class
privateprivate
protected
public
no
private
private
protectedprivate
protected
public
no
protected
protected
publicprivate
protected
public
no
protected
public

When you derive a derived class from a base class that has a constructor, you must call the constructor of the base type from the constructor of the derived class. Constructors are not inherited, so the derived class must have its own constructors. If there is no explicit call to the base type constructor in the constructor of a derived class. then it is called automatically without parameters. For multiple levels of inheritance, constructors are called starting at the top level. For multiple base types, their constructors are called in the order in which they are declared.

If there is a reference to the class described later in the class description, you just need to declare it using an operator.

class new_class;

This description is similar to the description of function prototypes.

Often, inheritance results in methods that work on different algorithms in different derived classes, but have the same output parameters and return value. Such methods are called virtual methods and are described using the service word virtual.

As an example, let's give an abstract base class figure (figure), on the basis of which you can build derived classes for real shapes (ellipse, circle, square, rhombus, triangle, etc.).

 

One of the main features of OOP is the ability to inherit. Inheritance is a way of reusing software in which new derived classes (inheritors) are created from existing base classes (parents).

When you create a new class, you inherit from the members and methods of the previously defined base class. The class created by inheritance is a derived class, which in turn can act as a base class for the generated classes. If the method names of the derived and base classes are the same, the methods of the derived class overload the methods of the base class.

When using inheritance, members and methods in addition to the public and private properties can have a protected property. For a single class, the protected and private specifiers are equivalent. The difference between protected and private is evident in inheritance. Private members and methods declared as protected in the base class can be used as public in the derived classProtected members and methods are an intermediate option between public and private.

When you create a derived class, use the following syntax:

1
2
3
4
5
6
7
8
9
10
11
12
class name_derived_class:
type_inheritance base_class
{
private methods and members of the ...

public:
public methods and class
members ...
protected:
protected methods and class members
...

};

Here, name_derived_class is the name of the derived class to be created, type_inheritance is the method of inheritance, the following methods of inheritance are possible: publicprivate, and protectedbase_class is the name of the base type. You should distinguish between the type of access to items in the base class and the type of inheritance.

Inheritance types and access types

Access MethodSpecifier in the base classAccess in a derived class
privateprivate
protected
public
no
private
private
protectedprivate
protected
public
no
protected
protected
publicprivate
protected
public
no
protected
public

When you derive a derived class from a base class that has a constructor, you must call the constructor of the base type from the constructor of the derived class. Constructors are not inherited, so the derived class must have its own constructors. If there is no explicit call to the base type constructor in the constructor of a derived class. then it is called automatically without parameters. For multiple levels of inheritance, constructors are called starting at the top level. For multiple base types, their constructors are called in the order in which they are declared.

If there is a reference to the class described later in the class description, you just need to declare it using an operator.

class new_class;

This description is similar to the description of function prototypes.

Often, inheritance results in methods that work on different algorithms in different derived classes, but have the same output parameters and return value. Such methods are called virtual methods and are described using the service word virtual.

As an example, let's give an abstract base class figure (figure), on the basis of which you can build derived classes for real shapes (ellipse, circle, square, rhombus, triangle, etc.).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68




#include "stdafx.h"
#include <iostream>
#include <math.h>
#define PI 3.14159;
using namespace std;
base class figure
class figure
{
public:
n-number of sides of the figure for a circle n=1
int n;
p-array of the lengths of the sides of the figure, for the circle in p the radius
float *p is stored;
figure() constructor
;
a method for calculating the perimeter of a float perimetr() shape
;
the method of calculating the area of
the figure virtual float square();
method of displaying information about the figure:
its name, perimeter, area, etc.
virtual void show_parametri();
};
the constructor of the
figure::figure()
{
cout<<" this is the abstract constructor "<<endl;
}
is a perimeter calculation method, it will only be
overloaded in the _circle
class float figure::perimetr()
{
int i;
float psum;
for (psum=0, i=0; i<n; psum+=p[i], i++)
return psum;
}
the method of calculating the area, as long as it is abstract, in
each class will be overloaded with the
real float method figure::square()
{
cout<<" The square figure is not abstract "<< endl;
return 0;
}
the shape output method will be
overloaded in each derived void class
figure::show_parametri()
{
cout<< "Abstract shape";
}
derived class _circle (circle) based</b1165<b1166>> class _circle figure _circle perimetr square show_parametri recTangle figure RecTangle square show_parametri main setlocaleLC_ALL, _circle RR RR. RecTangle PP PP. _circle_circle endl n p n endl p the _circle PIp _circle PIpp circle parameters _circle circleendl pendl the perimetrendl squareendl RecTangle endl n p n pp pp pp RecTangle pp RecTangle endl ppendl method perimeter = "perimetrendl squareendl
;
}