Inheritance refers to an object that uses properties and methods of another object directly.
The concept of inheritance is also used in programming, in object-oriented programming, such as C + + and Java have inheritance of classes
The total of C + + class inheritance can be implemented in three ways, including: public inheritance, private inheritance, protection Inheritance (protected), and so on. Inheritance can make existing code reusable and extensible. However, in C + + programming specifications (such as Google's programming specification), it is not recommended to use private inheritance and protect inheritance, but instead use a combination.
The C + + programming language has a wide range of applications and can help developers implement many features in a simple and flexible way. In C + + class inheritance, a derived class can derive from a base class or from more than one base class. Inheritance derived from a base class is called single inheritance, and inheritance derived from multiple base classes is called multiple inheritance.
Cases:
Definition of single inheritance
classb:publica{< new definition members for derived classes >};
Definition of multiple inheritance
classc:publica,privateb{< new definition members for derived classes >};
Note: If the access control is not written before the base class, the default is private
There are three types of C + + class inheritance in a derived class: public inheritance, private inheritance, protection inheritance (protected)
Public inheritance
The public and protected members of the base class can be public members and protected members of their derived classes
member functions of derived classes can access public and protected members in the base class and cannot access private members in the base class
The object of the derived class can access the public members of the base class
Private Inheritance (private)
Both public and protected members of the base class are private members of their derived classes
In private inheritance, a member of a base class can only be accessed by a direct derived class and cannot be inherited further down
Protect C + + class inheritance (protected)
All public and protected members of the base class become protected members of derived classes
The public and protected members of a base class can only be accessed by its direct derived class member function or friend
Constructors and destructors cannot be inherited
Therefore, when you construct an object of a derived class, you need to initialize the data members of the base class data members and member objects.
The parameter table portion of a derived class constructor requires both the initial value of some data member of the subclass and the initial value of the data member of the base class.
If the base class does not have a default constructor, the derived class must have a constructor that provides parameters to the base class constructor
The general syntax for a derived class constructor is as follows:
Derived class Name:: derived class name (list of parameters): base class name 1 (parameter Table 1),..., base class name N (parameter table N) {initialization statement} when the base class has more than one constructor, the compiler determines which constructor to call based on the parameter type provided by the derived class constructor for the parameter in the base class constructor
A destructor for a derived class should first be cleaned up by adding a normal member to the derived class
Then clean up the object members that are new to the derived class
Finally, all members that inherit from the C + + class are cleaned up
When a derived class object is created, if the base class has a constructor, the constructor of the base class is called first, then the constructor of the derived class is called, and the destructor is called in the reverse order when the derived class object is revoked
The public member in the class is inherited and is still a public member in the subclass.
The inheritance of classes in Java, C # is similar to C + +.
In object-oriented methodology, object-oriented is a very important concept, which is defined as: the object of a special class (or subclass, derived class) has all the properties and services of its general class (or parent class, base class), called the inheritance of the special class to the General class. It is the simulation of genetic phenomena in the real world, combining the data and the related methods of manipulating data, and developing the most basic reusable unit.
Inheritance in Java
Inheritance is an important mechanism of object-oriented language. With inheritance, you can extend the original code and apply it to other programs without having to rewrite the code. In the Java language, inheritance is implemented by extending the original class and declaring the new class. The new class of the extended declaration is called a subclass, and the original class is called the superclass (parent class). The inheritance mechanism stipulates that subclasses can have all the properties and methods of a superclass, or they can extend the definition of their own properties, add new methods, and redefine superclass methods.
In C #, subclasses cannot inherit private members of private classes, but they can access their public members.
Soft to quotations
Inherited:
Inheritance in software development is that subclasses can use properties and methods that inherit from the parent class.
Single Inheritance:
A single inheritance is a subclass that can have only one parent class
What does inheritance mean?