C + +: Inheritance

Source: Internet
Author: User

Inherited:

By inheriting the definition of a class, the relationships between their types are modeled, sharing things that are common, and implementing something different in their nature.

The "Inheritance" feature of C + + can improve the reusability of the program.

Inheritance definition Format:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/CA/wKiom1cIoCPD6ffCAAAkHgWtV5E225.png "title=" Base class parent class. png "alt=" Wkiom1ciocpd6ffcaaakhgwtv5e225.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/CA/wKiom1cIoACDo1XoAAB81ZKuMk8489.png "title=" Qq20160409142702.png "alt=" Wkiom1cioacdo1xoaab81zkumk8489.png "/>

Summarize:

1. Private members of the base class cannot be accessed in a derived class.

2. If the base class member does not want to be accessed directly outside the class, but needs to be accessible in the derived class, it is defined as protected. The protection member qualifier occurs because of inheritance.

3. Public inheritance is an interface inheritance that maintains the is-a principle, and the members that are available to each parent class are also available to the subclass, because each subclass object is also a parent class object.

4, Protetced/private Inheritance is an implementation of inheritance, some members of the base class are not completely part of the subclass interface, is the principle of has-a relations, so in non-special cases will not use the two inheritance relationship, in the vast majority of scenarios using the public inheritance.

5. Regardless of the inheritance method, the public and protected members of the base class can be accessed inside the derived class, and the private members of the base class exist but are not visible in the subclass (inaccessible).

6, the default way to inherit the keyword class is private, the default way to inherit the struct is public, but it is best to show the way to write the inheritance.

7, in the actual use is generally used in public inheritance, very few scenarios will use Protetced/private inheritance.

"Sequence of constructor calls in an inheritance relationship"

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/C6/wKioL1cIk5jwN5NOAAAscY_8knc587.png "style=" float: none; "title=" destructor. png "alt=" Wkiol1cik5jwn5noaaascy_8knc587.png "/>

Description

1. The base class does not have a default constructor, and the derived class must explicitly give the base class name and argument list in the initialization list.

Class Base

{

Public

Base (int data)

{  }

};

Class Base1

{

Public

BASE1 (int data)

{  }

};

Class Derive:public Base, public Base1

{

Public

Derive ()

: Base1 (Ten)

, Base (20)

{  }

};

2. If the base class does not have a constructor defined, the derived class can also use the default constructor without defining it.

3. The base class defines the constructor with the formal parameter list, and the derived class must define the constructor function.


"Sequence of destructor calls in an inheritance relationship"

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/C9/wKiom1cIkuijKCwFAAAooEarDD4689.png "style=" float: none; "title=" constructs a. png "alt=" Wkiom1cikuijkcwfaaaooeardd4689.png "/>


Scopes in the inheritance system:

1, in the inheritance system, the base class and the derived class are two different scopes.

2, subclasses and parent classes have members of the same name, and child class members will mask the parent class's direct access to members. (in subclass member functions, you can use the base class:: base class member access)--hide--redefine

3, note in practice in the inheritance system is best not to define the same name of the member.

Inheritance and transformation--assignment compatibility rule--public inheritance

1. Subclass object can be assigned to parent class object (cut/slice)

2. The parent object cannot be assigned to a child class object

3. Pointers/References to parent classes can point to child class objects

4. A pointer/reference to a subclass cannot point to the parent class object (can be done by forcing the type conversion)

Friends and inheritance

A friend relationship cannot inherit, which means that the base class friend cannot access the subclass private and protected members.

Inheritance vs. static members

The base class defines a static member, and there is only one such member in the entire inheritance system.


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/CA/wKiom1cImtTjhiZkAAAWqsxA9Mg290.png "style=" float: none; "title=" single inheritance. png "alt=" Wkiom1cimttjhizkaaawqsxa9mg290.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/CA/wKiom1cImtWAhDZ9AAAU-kwWhf0253.png "style=" float: none; "title=" multiple inheritance. png "alt=" Wkiom1cimtwahdz9aaau-kwwhf0253.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/CA/wKiom1cInM_CdaCOAAAeRTrpFZU333.png "title=" Diamond inheritance. png "alt=" Wkiom1cinm_cdacoaaaertrpfzu333.png "/>





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.