Resolve the use of inheritance in C++ programming

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

C++ inheritance and composition details
We know that you can use a class object as a data member in a class, that is, a child object (see: C++ constructor for a derived class with children). In fact, the type of an object member can be either the base class of this derived class or another defined class. A class whose data member is an object of another class in one class is called a combination of classes (composition).

For example, declare that the Professor(professor) class is a derivative of the Teacher(teacher) class, and another class, BirthDate(birthday), contains data members such as year,month,day, and so on. You can add information about the professor's birthday to the declaration of the Professor class. Such as:


class Teacher // The teacher class 
{
public:
  // Some Code
private:
  int num;
  string name;
  char sex;
};
class BirthDate // A class birthday 
{
public:
  // Some Code
private:
  int year;
  int month;
  int day;
};
class Professor:public Teacher // Professor class 
{
public:
  // Some Code
private:
  BirthDate birthday; //BirthDate The object of the class ACTS as a data member 
};

Class composition, like inheritance, is an important way to reuse software. Both composition and inheritance effectively leverage the resources of existing classes. But they have different concepts and USES. Through inheritance, the relation between derived class and base class is established, which is a kind of "yes" relation, such as "white cat is cat", "black man is man", and derived class is the embodiment of base class, which is one of the base class. The relationship between a member class and a composite class (or composite class) is established by composition. In this case, BirthDate is a member class and Professor is a composite class (which contains object members of another class in one class). It's not a "yes" relationship, it's a "yes" relationship. It cannot be said that the professor (Professor) is a birthday (BirthDate), only that the professor (Professor) has a birthday (BirthDate) attribute.

Professor class through inheritance, from Teacher class num, name, age, sex data members, such as through combination, from BirthDate class year, month, day data members, etc. Inheritance is vertical, composition is horizontal.

If the Professor object prof1 is defined, it is clear that prof1 contains birthday information. In this way, the existing classes are effectively organized and utilized, which greatly reduces the workload. If you have
 


  void fun1(Teacher &);
  void fun2(BirthDate &);

Call both functions in the main function:
   


 fun1(prof1); // Correct, the formal parameter is Teacher Class object, the argument is Teacher A subclass object of a class that is compatible with its assignment 
  fun2(prof1.birthday); // True, the real parameters are of the same type, both are BirthDate Class object 
  fun2(prof1); // Error, parameter requirement is BirthDate Class object and prof1 is Professor Type, no match 

If you modify part of a member class, the composite class may remain unchanged as long as the public interface of the member class (such as the header file name) remains unchanged, if not necessary. But the composite class needs to be recompiled.

The importance of inheritance in software development
Inheritance is an important part of object-oriented technology, which makes reuse of software possible.

In the past, software personnel to develop new software, can directly from the existing software to choose fully meet the requirements of the parts are not many, generally have to make a lot of changes to use, in fact, there is a considerable part to rewrite, work child is very large. The key to shortening the software development process is to encourage software reuse. The inheritance mechanism solves this problem. Writing object-oriented programs focuses on implementing classes that are useful to you, sorting and sorting existing classes, tailoring and modifying them, and then focusing on writing new additions to derived classes that can be used by many areas of programming. Inheritance is one of the important differences between C++ and C cockroaches.

Because C++ provides a mechanism for inheritance, this has attracted many vendors to develop a variety of useful libraries. Users use them as a base class to build their own classes (that is, derived classes) and design their own applications from there. The advent of class libraries has made software easier to reuse, and some are now sold to users along with the C++ compilation system. Readers should not think of class libraries as part of the C++ compilation system. Different C++ compilation systems provide different libraries developed by different vendors. In one C++ compilation system environment, a copysequence developed with the class library may not work in another C++ compilation system environment, unless the class library is migrated as well. Considering the large number of users, the libraries currently provided with the C++ compilation system are relatively general, but their pertinence and practical scope are also limited. With the rapid global adoption of C ++, there is a growing effort to develop libraries around the world for use in all areas.

The declaration of a class in a library is usually in a header file, and the implementation of the class (the part where the function is defined) is compiled separately, stored as object code in a directory on the system. The user does not need to know the source code when using the library, but must know how to use the header file and how to connect the object code (in which subdirectory) so that the source program can connect to it after compilation.

Since the base class is compiled separately, you only need to compile the new functionality of the derived class when the program is compiled, which greatly improves the efficiency of debugging the program. If a base class is modified if necessary, the derived class need not be modified as long as the common interface of the base class is unchanged, but the base class needs to be recompiled, and the derived class must be recompiled, otherwise it will not work.

So why is inheritance so important, requiring the use of inheritance in software development to create as many new classes as possible through inheritance? Why not modify existing classes to suit your application?

In summary, there are several reasons:
There are many base classes that are used by other parts of the program or other programs that require that the original base class be left intact. Using inheritance is to create a new data type that inherits all the characteristics of the base class without changing the base class itself. The name, composition, and access properties of the base class have not changed at all and do not affect the use of other programs.
Users often do not get the source code for the base class. If you want to modify an existing class, you must know the class declaration and the class implementation (the definition of the member function) of the source code. However, if you use a class library, the user has no way of knowing the code for the member function, and therefore no way to modify the base class.
In a class library, a base class may have been specified to establish a relationship with multiple components required by the user, so the base class in the library is not allowed to be modified (even if the user knows the source code).
In fact, many base classes are not selected from existing programs but are designed specifically as base classes. Some base classes may not have any independent functionality, just a framework, or an abstract class. A set of generic classes that can be used for different purposes has been designed to create a common data structure on which users can add various functions and thus create derived classes of various functions.
In object-oriented programming, we need to design the hierarchy of classes, starting from the original abstract class, the establishment of derived classes at each level is gradually towards the concrete realization of the target, in other words, it is a process from the abstract to the concrete. Each level of derivation and inheritance needs to be planned and organized from the perspective of the entire system.


Related articles: