C + + inheritance and combination of detailed explanation
We know that in a class you can use a class object as a data member, a child object (see: C + + A constructor for a derived class that has child objects). In fact, the type of an object member can be the base class for this derived class, or it can be another defined class. In a class, the object of another class as a data member, called a combination of classes (composition).
For example, the declaration Professor class is a derived class of the teacher (teacher) class, and another class birthdate (birthday) contains data members such as Year,month,day. You can add information about a professor's birthday to the Professor class declaration. Such as:
Class Teacher//Teacher classes
{public
:
//Some Code
private:
int num;
string name;
char sex;
};
Class Birthdate//Birthday category
{public
:
//Some Code
private:
int year;
int month;
int day;
Class Professor:public Teacher//Professor classes
{public
:
//Some Code
Private:
birthdate birthday;// The object of the Birthdate class as a data member
};
The combination of classes and inheritance is an important way to reuse software. Combination and inheritance are resources that effectively utilize existing classes. But the concepts and usages of the two are different. The relationship between a derived class and a base class is established by inheritance, which is a "yes" relationship, such as "White Cat is a cat", "Black Is Human", and derived classes are materialized implementations of base classes and are one of the base classes. The relationship between the Member class and the combined class (or compound Class) is established by combining, in this case birthdate is a member class, and professor is a composite class (an object member that contains another class in one class). They are not "yes" relationships, but "there" relationships. It cannot be said that the Professor (professor) is a birthday (birthdate), only that the Professor (professor) has a birthday (birthdate) attribute.
By inheriting, the Professor class obtains data members such as Num,name,age,sex from the teacher class, and obtains data members such as Year,month,day from the Birthdate class by combining. Inheritance is portrait, and the combination is horizontal.
If you define a Professor object Prof1, it is clear that Prof1 contains information about birthdays. This method effectively organizes and utilizes the existing classes, greatly reducing the workload. If there is
void Fun1 (Teacher &);
void Fun2 (birthdate &);
Call these two functions in the main function:
FUN1 (PROF1); Correct, the parameter is a reference to the Teacher class object, the argument is a subclass object of the teacher class, and is assigned a compatible
fun2 (prof1.birthday);//correct, the actual participating formal parameter type is the same as the Birthdate class object
fun2 ( PROF1); Error, parameter request is Birthdate class object, and Prof1 is Professor type, does not match
If you modify part of a member class, if the public interface (such as the header file name) of the member class does not change, the composition class can be unmodified. But the composite class needs to be recompiled.
The importance of inheritance in software development
Inheritance is an important content of object-oriented technology, and inheritance makes it possible to reuse software.
In the past, software personnel to develop new software, from the existing software directly select the full compliance with the requirements of the components are not many, generally have to make a lot of changes to use, in fact, a considerable part to be rewritten, working children very large. The key to shortening the software development process is to encourage software reuse. The inheritance mechanism solves the problem. When writing object-oriented programs, focus on implementing the classes that are useful to you, sorting and sorting existing classes, tailoring and modifying them, and concentrating on writing the new additions to the derived classes so that they can be used in many areas of programming. Inheritance is one of the important differences between C and C's cockroaches.
Because C + + provides an inheritance mechanism, this attracts many vendors to develop a variety of useful class libraries. Users use them as base classes to build their own classes (that is, derived classes) and design their own applications on this basis. The appearance of class library makes software reuse more convenient, now some class libraries are sold to users with C + + compilation system. Readers should not think of class libraries as part of a C + + compilation system. Different C + + compilation systems provide a different class library developed by different vendors. In a C + + compilation system environment, the use of class library development of the 稈 sequence, in another C + + compilation system environment may not work, unless the class library is also ported to the past. Considering the situation of the vast number of users, the current with C + + compiler system to provide the class library is more general, but its targeted and practical scope is also limited. With the rapid promotion of C + + worldwide, the development of class libraries in various fields around the world is becoming increasingly prosperous.
The declaration of a class in a class library is generally placed in a header file, and the implementation of the class (the definition part of the function) is compiled separately and stored in a directory of the system in the form of the object code. When users use class libraries, they do not need to know the source code, but they must know how to use the header file and how to connect the target code (in which subdirectory) so that the source program is connected to it after compiling.
Because the base class is compiled separately, only the new functionality of the derived class is compiled at compile time, which greatly increases the efficiency of the debugger. If the base class is modified if necessary, the derived class does not have to 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 do people value inheritance so much that it requires the use of inheritance in software development to build a batch of new classes as far as possible through inheritance? Why not modify an existing class to meet the requirements of your application?
Summed up for several reasons:
There are many base classes that are used by other parts of the program or by other programs that require that the original base class be preserved from being corrupted. Using inheritance is creating a new data type that inherits all the characteristics of the base class, but does not change the base class itself. The name, composition, and access properties of the base class do not change in the slightest, 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 master the source code for the declaration of the class and the implementation of the class (the definition of the member function). However, if you use a class library, the user cannot know the code of the member function, and therefore cannot modify the base class.
in a class library, a base class may have been assigned a relationship with a variety of components required by the user, so the base class in the class library is not allowed to be modified (even if the user knows the source code and never allows it to be modified).
In fact, many of the base classes are not selected from existing other programs, but are specifically designed as base classes. Some base classes may not have any independent functionality, just a framework, or abstract class. A number of generic classes that can be used for different purposes are designed to create a generic data structure that allows users to add a variety of features to build a variety of functional derived classes.
in object-oriented programming, the hierarchy of classes needs to be designed, starting with the original abstract class, and each layer of derived classes is gradually progressing toward the concrete realization of the goal, in other words, from abstraction to concrete. Each layer of derivation and inheritance needs to stand in the overall system of the perspective of unified planning, carefully organized.