Interpret the composition and creation of derived classes in C++ programming

  • 2020-05-05 11:35:45
  • OfStack

C++ derived classes constitute
The members of a derived class consist of members inherited from the base class and members added by themselves. The members inherited from the base class reflect the generality of the derived class inherited from the base class, while the newly added members reflect the personality of the derived class. It is these new additions that show the difference between derived classes and base classes, and the difference between derived classes.

There are two parts in the base class: data members and member functions (or data and methods), and derived classes are divided into two parts: one is the member that is inherited from the base class, and the other is the part that is added when the derived class is declared. Each section includes data members and member functions, respectively.

In fact, it is not simply adding the members of the base class and the members of the derived class that are added by the derived class itself. Constructing a derived class consists of the following three parts of the job.

1) receive members

from the base class

Derived classes receive all the members of the base class (excluding constructors and destructors), that is, they are not selected, so they cannot choose to receive some members and discard others. It is not optional from defining the general form of a derived class.

It is possible that some members of a base class are not available in a derived class, but must be inherited. This leads to redundant data, especially after multiple derivations, where there is a lot of useless data in many derived class objects, which not only wastes a lot of space, but also wastes a lot of time in object creation, assignment, replication, and parameter passing, thus reducing efficiency. This is not addressed in the current C++ standard, which requires careful selection of base classes based on the needs of derived classes to minimize redundancy. Instead of randomly building derived classes from one of your existing classes as a base class, consider how you can make your derived classes more structurally sound. In fact, some classes are designed specifically as base classes, with the requirements of derived classes in mind.

2) adjust the members

received from the base class

Receiving base class members is not optional by the programmer, but the programmer can make some adjustments to these members. For example, you can change the access properties of a base class member in a derived class by specifying inheritance. If a public member of a base class can be specified by inheritance as having an access property private in a derived class (not accessible outside a derived class). In addition, you can declare a member with the same name as a base class member in a derived class, and the new member in the derived class overrides the base class member with the same name. Note, however, that if it is a member function, not only should the name of the function be the same, but the list of arguments to the function (the number and type of arguments) should also be the same, if not, it becomes an overload of the function rather than an overwrite. In this way, you can replace members of a base class with new members.

3) member

added when declaring derived classes

This section is important because it shows how derived classes extend the functionality of base classes. Carefully consider which members should be added as needed, and design carefully. Such as in the previous example (see: C + + derived class declaration form), a base class display function is the function of the output of student id, name and gender, and asked the output in a derived class student id, name, gender, age, and address, without having to write another output individually this five data function, and to make use of the base class display function output student id, name and gender, in addition to define a display_1 function output age, and address, successively to perform both functions. You can also call the display function of the base class in the display_1 function, output two more data, and just call the display_1 function in the main function, which may be clearer and easier to read.

In addition, when declaring derived classes, you should generally define your own constructors and destructors for derived classes, since constructors and destructors cannot be inherited from base classes.

As you can see from the above introduction, derived classes are a continuation of the base class definition. You can declare a base class that provides only some of the most basic functionality, while others are not implemented, and then add some specific functionality when you declare a derived class to form a derived class for a particular application. By declaring a continuation of the base class, an abstract base class is transformed into a concrete derived class. Therefore, derived classes are concrete implementations of abstract base classes.

C++ the way derived classes are declared (defined)
Let's start with an example of how to create derived classes through inheritance, starting with the simplest single inheritance.


class Student1: public Student // Declare that the base class is Student
{
public:
  void display_1( ) // New member function added 
  {
   cout<<"age: "<<age<<endl;
   cout<<"address: "<<addr<<endl;
  }
private:
  int age; // New data members added 
  string addr; // New data members added 
};

Assuming that a base class Student has been declared (the base class Student is defined in the above section: C++ concept of inheritance and derivation), a derived class Student1:
is created by single inheritance Look closely at the first line:


  class Student1: public Student


Student1 after class is the new class name, and Student after the colon is the declared base class. Before Student there is a key public that represents how members of the base class Student inherit in the derived class Studeml. Those with public in front of the base class name are called "common inheritance" (public inheritance).

Please read the above declared derived class Student1 and the base class Student carefully and put them together for analysis.

The general form of declaring a derived class is:
     


 class  Derived class name : [inheritance style]   The base class name 
  {
     Newly added member of a derived class 
  };

Inheritance methods include public (public), private (private), and protected(protected).


Related articles: