You get a bonus - 1 coin for daily activity. Now you have 1 coin

Chapter 2. Inheritance, polymorphism and virtual functions 2.1. Derived Class Definition

Lecture



Inheritance is a mechanism for obtaining a new class based on an existing one. An existing class can be added or modified to create a new class.

Existing classes are called basic , and new classes are derived . The derived class inherits the base class description; it can then be modified by adding new members, changing existing member functions and changing access rights. Thus, inheritance allows you to reuse the already developed code, which improves the programmer's performance and reduces the likelihood of errors. With the help of inheritance, a hierarchy of classes can be created that share code and interfaces.

Inherited components are not moved to the derived class, but remain in the base classes. A message that cannot be processed by a derived class method is automatically transferred to the base class. If you need data that is missing in the derived class to process the message, then they are trying to find it automatically and imperceptibly for the programmer in the base class. When inheriting, some method and data names of the base class can be redefined in the derived class. In this case, the corresponding components of the base class become unavailable from the derived class. To access them, the operation is used to specify the scope '::' .

In a hierarchy, a derived object inherits the components of all base objects ( public, protected ) that are allowed for inheritance.

Multiple inheritance is allowed - the ability for some class to inherit the components of several unrelated base classes. In the class hierarchy, the agreement regarding the availability of class components is as follows:

private - A member of a class can only be used by member functions of a given class and the “friends” functions of its class. It is not available in the derived class.

protected - Same as private, but in addition a member of a class with a given access attribute can be used by member functions and “friends” of classes derived from this one.

public - A member of a class can be used by any function that is a member of a given or derived class, and also public members can be accessed externally through the name of an object.

Note that the friend declaration is not an access attribute and is not inherited.

Definition of the derived class syntax:

class class_name: base_class_list

{class_component_list};

In a derived class, inherited components get private access status if a new class is defined using the class keyword, and public status if using a struct

For example.

a) class S: X, Y, Z {. . .};

S is a derived class;

X, Y, Z - base classes.

Here, all inherited components of the classes X, Y, Z in class A receive the access status private.

b) struct S: X, Y, Z {. . .};

S is a derived class;

X, Y, Z - base classes.

Here, all inherited components of the classes X, Y, Z in class A are given public access status .

To explicitly change the default access status during inheritance, you can use access attributes - private, protected and public , which are specified immediately before the names of the base classes. How access attributes change in a derived class is shown in the following table.

attribute specified by inheritance
attribute in base class
attribute derived in derived class
public

public

protected

public

protected

protected

public

protected

protected

protected

private

public

protected

private

private

Example 2.1.1


Example 2.1.1

class B

{protected: int t;

public: char u;

private: int x;

};

struct S: B {}; // inherited members t, u have an access attribute public

class E: B {}; // t, u have the access attribute private

class M: protected B {}; // t, u - protected.

class D: public B {}; // t - protected, u - public

class P: private B {}; // t, u - private

Thus, you can only narrow the area of ​​access, but not to expand.

Thus, within a derived class, there are four levels for which an access attribute is defined:

· For members of the base class;

· For members of the derived class;

· For the inheritance process;

· To change attributes during inheritance.

Consider how this controls access to class members from outside the class and inside the class.

Access from the outside.

Only elements with the public attribute are available .

· Own class members.

Access is governed only by the access attribute specified in the class description.

· Inherited class members.

Access is determined by the access attributes of the base class, is limited to the access attribute during inheritance, and is changed by explicitly specifying the access attribute in the derived class.

Example 2.1.2

Example 2.1.2

class Basis {

public:

int a, b;

protected:

int c;};

class Derived: public Basis {

public:

Basis :: a;};

void main () {

Basis ob;

Derived od;

ob.a; //right

ob.c; //mistake

od.a; //right

od.b; //mistake

Access from within.

· Own class members.

Access from outside is only possible for public members of a class.

private and protected members of a class can only be used by member functions of this class.

· Inherited class members.

private - class members can only be used by their own member functions of the base class, but not by the members of the derived class.

A protected or public member of a class is available for all member functions. The division into public, protected and private refers here to the descriptions given in the base class, regardless of the form of inheritance.

Example 2.1.3

Example 2.1.3

class Basis {

int a;

public b;

void f1 (int i) {a = i; b = i;}

class Derived: private Basis {

public:

void f2 (int i) {

a = i; //mistake

b = i;} // // correctly

};

See also


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

C ++ (C plus plus)

Terms: C ++ (C plus plus)