C + + Inheritance

Source: Internet
Author: User

Inheritance (inheritance) is one of the main object-oriented features (in addition to encapsulation and polymorphism), which enables a class to derive from an existing class without having to redefine a new class. The essence of inheritance is to create new data types with existing data types and preserve the characteristics of their own data types, create new classes based on old classes, new classes contain data members and member functions of the old class, and new data members and member functions can be added to the new class. The old class is called the base class or parent class, and the new class is referred to as a derived class or subclass.

Inheritance is also part of the class mechanism in the C 10 + language, which enables a hierarchy to be established between classes and classes. You can create a new class by providing operations and data members from another class, and the programmer simply defines the components in the new class that are not in the class. This is another masterpiece of abstract programming of classes that follow the nature of data encapsulation and information hiding. With inheritance, the code of the past can be discarded and reused as long as it is slightly modified. The development of human beings from the lower society to the advanced society, the level of civilization and the knowledge gained, in fact, the development of things is always a low-to-high development process. Class inheritance is a process that reflects the gradual evolution of primitive simple code into rich, high-level code. The program is more and more perfect, the function is more and more strong, people not through the external code copy and save, but through the inherent function of the language, automatically and rolling reuse code, enhance the code, so that the method of programming begins to change fundamentally, the analysis problem and problem solving mode from functional mode to object structure mode.

Derived classes are always dependent on the base class, and the derived class object always contains the base class object, which is the data member that contains the base class. Or, a base class object is part of a derived class object. As for the arrangement of space in specific implementations, it is not necessarily the base class that is ranked before the derived class. Obviously, the derived class object must not be smaller than the base class object, the base class is also called the superclass, and the derived class is also called a subclass, which holds more data and provides more operations.

1. The inheritance class inherits from the following form:

Class derived class name identifier: [Inheritance Method] base class name identifier
{
[Access control modifier:]
[Member declaration list]
};

There are 3 types of inheritance, namely, public type, protected type (protected), and private type (private), and access control modifiers are public,protected,private 3 types; The member declaration list contains member variables and member functions for the class, and is a new member of the derived class. ":" is an operator that represents the inheritance relationship of a base class and a derived class.

Public inheritance, which reflects the full acceptance of the use of the base class by the derived class, is expanded so that it can be used more widely by the outside world. Therefore, by deriving class objects, you can still use the original public operation in the base class. Derived classes obtain inheritance of access control permissions that are intact for all base class members. Inherits the base class, not that the derived class can access the private members of the base class. If that is the case, those who use the base class, by derivation, can reach the perverted purpose of accessing the private members of the base class.

In a class, there is also a protection (protected) type of access control, which, like a private member, cannot be accessed publicly by a programmer using the class, but can be accessed by a member function inside the class. In addition to the class used is a derived class member, it can be accessed, which is the ability that a private member does not have. This means that if a class member is declared as a protected member, its derived class can inherit the public and protected operations of its parent class.

2. Post-Inheritance accessibility

There are public, private, protected 3 types of inheritance, which are described as follows:

Public (co-derived)
A common derivation represents a public data member and member function in the base class, still public in a derived class, and private in a derived class for private data members and member functions in a class.

Private (privately derived)
The private derivation represents the public in the base class, protected data members, and member functions that can be accessed in derived classes. Private data members in a base class that are not accessible in derived classes.

Protected (protected type derivation)
The protected derivation represents the public in the base class, protected data members and member functions, which are protected in the derived class. The protected type can be accessed when the derived class is defined, and objects declared with derived classes are not accessible, that is, outside the class body. Protected members can be used by all derived classes of the base class. This property can propagate infinitely downward along the inheritance tree.

Because the internal data of the protection class cannot be changed arbitrarily, the instance class itself is responsible for maintenance, which plays a very good role in encapsulation. The division of a class into two parts, part of the public, the other is protected, the protection of the members of the user is not visible, but also do not need to understand, which reduces the class and other code associated with the degree. The function of the class is independent, it does not depend on the application's running environment, it can be used in this program, can also be put into that program. This makes it very easy to replace another class with one class. The protection mechanism of class access restrictions makes the application more reliable and maintainable.

3. Construction of derived classes

A derived class is also a class, and if a constructor is not defined, the default parameterless constructor is executed according to the class mechanism. The default parameterless constructor for a derived class first calls the parent class's parameterless constructor, which causes the compilation to become angry if the parent class defines a parameter constructor (hence no default parameterless constructor), and no overloaded definition parameterless constructor. If the parent class also has a parent class, the parent class will first call the parameterless constructor of the parent class, recursively.

When constructing a subclass, the construction of its base class part is done by the constructor of the base class, and the base class object is treated as an object completely independent of the derived class. The advantage of this is that once the implementation of the base class has errors, the modifications in the base class implementation do not affect the operation of the derived class as long as the interface is not involved. Between classes and classes, you do yours, I do mine, and the responsibilities are clear, even among classes that have parent-child inheritance relationships.

Because there are constructors and destructors in both the parent and child classes, when you derive a subclass from a parent class and declare an object of a subclass, it invokes the constructor of the parent class and then calls the constructor of the current class to create the object, and when the subclass object is disposed, the destructor for the current class is called first, and then the destructor for the parent class.

After parsing the object's build and release process, it is considered that a pointer to a base class type is defined, the constructor of the subclass is called to construct the object, and when the object is disposed, is it called the destructor of the parent class or the destructor of the subclass first, and then the destructor of the parent class is called? The answer is that if the destructor is a virtual function, the destructor of the subclass is called first, and then the destructor of the parent class is called, and the destructor of the parent class is called only if the destructor is not a virtual function. As you can imagine, if space is allocated to a data member in a subclass in the heap, the destructor in the parent class is not a virtual member function, and the destructor of the subclass is not called, and the result is that the object cannot be freed correctly, resulting in a memory leak. Therefore, when you write a destructor for a class, the destructor is usually a virtual function. The order of the constructor calls is not affected by the presence of the base class in the member initialization table and the order in which it is listed.

C + + Inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.