Reference: Http://www.weixueyuan.net/view/6358.html,
Summarize:
Inheritance can be understood as a procedure in which a class obtains methods (functions) and properties (member variables) from another class.
An inherited class is called a parent class or a base class, and an inherited class is called a subclass or derived class.
Since the derived class can inherit the code of the base class without redesign, inheritance solves the problem of code reuse and greatly improves the development efficiency of the software.
With public inheritance, the properties of the base class's public member variables and member functions inherit from the derived class without changing.
If inheritance is not named in the inheritance process, the compiler system inherits the method by default as private or protected property.
Although we can inherit the private members of the book class, the private member variables of the book class are not directly accessible in the derived class and can only be accessed indirectly.
-----------------------------------
Inheritance is the relationship between classes and classes, and is a very simple and intuitive concept, similar to inheritance in the real world (such as a son inheriting a father's property).
inheritance can be understood as a procedure in which a class obtains methods (functions) and properties (member variables) from another class. If Class B inherits from Class A, then B has the method and properties of a . An inherited class is called a parent class or a base class, and an inherited class is called a subclass or derived class.
In the previous chapter we gave some examples of book class related, with two member variables: Title and price, which are the names and prices of books. If we were to classify these books in the programming language at this point, we would need a third member variable language, at which time we would not need to redesign a new class, just add the language attribute and its associated methods on the book class basis.
At this point, we can inherit the Codingbook class we need to the book class to get the book class member variables and member functions, after inheritance in the need to add the language properties and related methods. The new class Codingbook is called a derived class or subclass of the book class, whereas the original book class is called the base class. Derived classes Codingbook can have language member variables and their associated member functions that are not in the base class, except for all data members and member functions that can have a base class book.
because the derived class can inherit the code of the base class without redesign, inheritance solves the problem of code reuse, and greatly improves the development efficiency of the software . At the same time, if the code runs correctly in the base class, it will not be problematic to run after inheriting to the derived class.
Inheritance is not only a layer, it can be any level. For example, Codingbook inherits from the book class, and if we need to continue adding a new property to the Codingbook class, we can also design a class that inherits from the Codingbook class.
The syntax for deriving the Codingbook class from the book class is shown in the following example.
Example 1:
enumLanguage{cpp, Java, Python,javascript, PHP, Ruby};classbook{ Public: voidSetprice (Doublea); DoubleGetPrice ()Const; voidSettitle (Char*a); Char* GetTitle ()Const; voiddisplay ();Private: DoublePrice ; Char*title;};classCodingbook: Publicbook{ Public : voidSetlang (language Lang); Language Getlang () {returnLang;}Private: Language lang;};
For the sake of convenience, we first declare a global enumeration type language before the class definition, which is used to represent the programming language. Book class We are already familiar with, the key is the definition of the Codingbook class. There is no difference between the definition of the Codingbook class and the class definition method described in the previous chapter when the Codingbook class is defined with ": Public book". where the keyword public indicates that the inheritance method belongs to the common inheritance, book is the inherited class name. with public inheritance, the properties of the base class's public member variables and member functions inherit from the derived class without changing. For example, after the public Setprice and Settitle member functions of the book class have been inherited to the Codingbook class, the properties of the two member variables will still be publicly properties. if inheritance is not named in the inheritance process, the compiler system inherits the method by default as private or protected property.
In this case, because a book class has been defined, it has the basic properties of the Books class: the title and the price. Now we need a new class Codingbook to describe the programming class books, for which we inherit all the members and member functions in the book class, and add the Language property and the corresponding operation function. Although we can inherit the private members of the book class, the private member variables of the book class are not directly accessible in the derived class and can only be accessed indirectly. indirect access is implemented through the GetPrice, GetTitle, Setprice, and Settitle functions, because these functions are public properties in derived classes . A list of all members in the Codingbook class is shown in the following table.
Codingbook Class Members |
member Properties |
Source |
Price |
Not accessible |
Inherit from book Class |
Title |
Not accessible |
Inherit from book Class |
Lang |
Private |
Codingbook New |
Setprice |
Public |
Inherit from book Class |
Settitle |
Public |
Inherit from book Class |
GetPrice |
Public |
Inherit from book Class |
GetTitle |
Public |
Inherit from book Class |
Dispaly |
Public |
Inherit from book Class |
Setlang |
Public |
Codingbook New |
Getlang |
Public |
Codingbook New
|
3.1 The concept and syntax of C + + inheritance