One of the three main features of C + + inherits

Source: Internet
Author: User

I. Basic concepts related to inheritance1, the definition of inheritancein C + +, inheritance can be used to get the new class to derive attributes from some of the classes that have already been defined, which is like having a child inherit from the parent, so we call the original class the base class or the parents, and the process of generating the new class with the original class is called derivation, so the resulting new class is called a derived class or subclass 2. Declaration of Successionin inheritance and in the above mentioned heredity is different, the child can only inherit some genes of their parents, and in C + + inheritance, a new class can inherit many different classes, known as multiple inheritance. So inheritance is divided into single inheritance and multiple inheritance. the inherited definition format is as follows:In a class, we know that the type of access for its members is divided into three types, public, protected (protected), and private. The corresponding inheritance methods are also divided into these three, the difference is that the members of the derived class and the object outside the class access it from the base class inherited from the members of the access rights. The following table shows the transformation relationships of member access rights after three inheritance methods:      In fact, this table I am looking very annoying, in fact, it is easy to remember, there is no need to look at this table. You just remember that the private members of the base class are always inaccessible in the derived class, and then remember that private>protected>public, the way high access permissions are inherited, alters the access rights of the lower access members.       Say or more around the mouth, in fact, do not need to remember, you will certainly remember in mind, do not believe you try. As you can see,regardless of the inheritance method, the non-private members of the base class can be accessed within the derived class, although the private members of the base class are inherited, but not visible. For protection and private inheritance, an out-of-class object cannot access the members of the base class, and public inheritance can access the public members of the base class. Ii. Constructors and destructors       in a derived class, member initializations inherited from a base class need to call the constructor of the base class, and the new member initialization in the derived class calls its own constructor.   1. Constructors in inheritance           Here, when a base class does not define its own constructor, it can also be used in a derived class without a definition, invoking the default constructor on its own when constructing the object. But generally in the compiler will think that the constructor does not do anything, so the code is optimized, does not generate a default constructor, when you go to disassembly to see the assembly code is not see the call constructor statement.       When the base class explicitly defines its own constructor, the compiler generates a default constructor for the derived class, and when the base class does not have a default constructor, the constructor must be defined for the derived class and the base class name and its argument list are given explicitly in the initialization list. Otherwise, the compiler does not know how to call the base class constructor during the derivation process.        The above two points are in fact similar to the case of objects that contain another class in the class, which we can easily understand. So can the derived class inherit the constructor of the base class?         That's a lot of people saying differently, some say that a derived class inherits the constructor of the base class, because you call the constructor of the base class when you construct a derived class object. In fact, it should be the quoted inheritance, the reason that the constructor of the base class can be called because the compiler makes the constructor of the base class visible in the derived class, and the constructor of the base class is called first when the object of the derived class is created.   Constructor Call order:      Because when you create a derived class object, you first give it a member of the base class, so when the program walks to the constructor of the derived class, it calls the constructor of the base class, so for the constructor call order, is to call the constructor of each base class sequentially in the order in which the list is inherited, and then call the derived class's own constructor, and then execute its function body. Of course, if there are other class objects in the derived class, call the constructor of the object class first and then call the derived class's own constructor.         2. Destructors in inheritance         destructors cannot be inherited, and the destructor of the base class is generally defined as a virtual destructor:
1 classBase2 {3  Public:4 Base () {}5     Virtual~Base ()6     {7cout <<"~base ()"<<Endl;8     }9  Public:Ten     int_pub1; One }; A classDerived: PublicBase - { -  Public: theDerived (intk=1) -     { -BUF =New Char[K]; -     } +~Derived () -     { +         Delete[] buf; Acout <<"~derived ()"<<Endl; at     } -  Public: -     Char*buf; - }; -   - voidTest () in { -   tobase* B =NewDerived (5); +     Deleteb; - } the intMain () * { $ test ();Panax Notoginseng     return 0; -}
If you do not define a virtual function, a memory leak is caused by only calling the destructor of the base class at Delete and not invoking the destructor of the derived class. The virtual function is not detailed here. The call order of destructors: the invocation of destructors, we generally think similar to the compression stack, so the destructor call order is as follows: Third, the same name in the inheritance is hidden in C + + We know there is overloading, when the function name is the same in the same scope and the function's argument list is different, it will be overloaded, so you can        The corresponding function is called according to the difference of the parameters, and there is no ambiguity. In a derived class, however, if there is a function with the same name in the base class, the function of the base class in the derived class is masked, and when you call this function with a derived class object, you must call the function in the derived class, even if the argument list of the two functions is different. However, the function of the base class is inherited, and the call can be invoked using the scope resolver. (The same is true for a member of the same name). Examples are as follows:
1 classA2 {3  Public:4     voidTestint)5     {6cout <<"test1 ()"<<Endl;7     }8  9  Public:Ten     int_pub1; One protected: A     int_pro1; - Private: -     int_pri1; the }; -   - classD | PublicA - { +  Public: -     voidTest () +     { Acout <<"test2 ()"<<Endl; at     } -  Public: -     int_pub2; - protected: -     int_pro2; - Private: in     int_pri2; - }; to intMain () + { - b b; theB.test ();//Compile Error *B.test (3);//Compile Error $B.a::test (3);//correctPanax Notoginseng     return 0; -}
Iv. assignment compatibility rules in inheritance before we look at the object model of the base class and the derived class:
1 classBase2 {3  Public:4Base () {cout <<"Base ()"<<Endl;}5~base () {cout <<"~base ()"<<Endl;}6  Public:7     int_pub1;8 protected:9     int_pro1;Ten   One }; A classDerived: PublicBase - { -  Public: theDerived () {cout <<"Derived ()"<<Endl;} -~derived () {cout <<"~derived ()"<<Endl;} -  Public: -     int_pub2; + protected: -      int_pro2; + }; A intMain () at { -     return 0; -}

If you define a base class and a derived class as such, the derived class inherits the _pub1 and Pro1 members of the base class.The assignment compatibility rules are as follows:1. Derived class objects can be directly assigned to base class objects       base class objects cannot be assigned to a derived class object 2, a base class type pointer can point to a derived class object (a derived class object can initialize a reference to a base class)       A derived class type pointer can not point to a base class object here is also easier to understand (of course, under the public inheritance), a derived class object is inherited from its base class, to the object model, in the derived class has a module corresponding to the base class is inherited from the member, Then in the assignment process the compiler can assign the corresponding base class part to the base class object. And for assigning a base class object to a derived class object, five, understand "is a" and "has a"       I didn't understand why I had to summarize this kind of relationship in the beginning, so abstract, something. Later write code also did slowly grasp a little, or there is a silk charm in it. is a:     is A There is one, for public inheritance, there is a feature is a. In the assignment compatibility rules above, it is also mentioned that a derived class object can be assigned to a base class object, so a derived class can replace any place that requires a direct base class. Is a represents this kind of inheritance relationship. For multiple inheritance or for new additions in derived class objects, this relationship is the equivalent of IS. Has a:     has A is generally used to describe the combination of this relationship, that is, there is another class in one class. Then in this class you can use the members and member functions of the classes that it contains, and sometimes we can think of protection and private inheritance as a has relationship, because members of the base class can be accessed only within the class.      is A is the equivalent of a father working at his son's house, and has a is equivalent to hiring someone to work at home. This is probably the meaning, we can understand the line, for the combination and inheritance here do not talk about, they have advantages and disadvantages and useful. When we take advantage of inheritance, it is not that we need to use something from another class in this class to inherit it, to ensure that the class is a relationship with the base class, such as the Tiger is a kind of animal, this is called inheritance.                                

One of the three main features of C + + inherits

Related Article

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.