C + + private, shared, protected member inheritance way __c++

Source: Internet
Author: User

if the class is an essential part of these modern programming languages, including C + + and Java, then inheritance and derivation are related to the same life and death. By inheriting the classes and objects to adapt to the changing problem domain, as much as possible reuse existing classes and objects, to transform and expand them, improve the efficiency of program design. Because members have different permissions, their access rights will change as they inherit, and then they will be introduced.


* Inherit

inheritance is a development relationship between a class and a class, and the inherited class is called a base class (or a parent class), and a class of two inheriting base classes is called a derived class (or subclass). In the book "C + + Language Programming Course" published by Tsinghua University Press, it is pointed out that through inheritance, a new class is derived from the original, defined class, inherits the properties and behavior of the original class, and can extend new properties and behavior, or update the members in the existing class.


* Inheritance Category:
Because there are complex relationships between classes and classes, naturally, inheritance will have different ways, depending on the number of inheritance can be divided into single inheritance and multiple inheritance, each derived class has and only one base class is called single inheritance, if a derived class has two or more than two base class is called multiple inheritance.

diagram:




* Inheritance mode:

inheritance has public inheritance, private inheritance, and protection inheritance. the base class and the member properties of the derived class after the use of different inheritance methods in the base class and derived classes are shown in the following table

properties in the base class

Inheritance Way

How to access in a derived class

Public Members


Total Inheritance

Public

Protect Members

Protect

Private Members

not directly accessible

Public Members


Protect Inheritance

Protect

Protect Members

Protect

Private Members

not directly accessible

Public Members


Private Inheritance

Private

Protect Members

Private

Private Members

not directly accessible


* Derivation

A derived class owning an attribute of a base class is called inheritance, and the process of deriving a derived class from a base class is called a derivation. Inheritance and derivation are a pair of same life and death relationship.

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.