C ++ FAQ Lite [19] - Inheritance (Basis) (Update)

zhaozj2021-02-08  214

[19] Inherited - Basis (Part of C FAQ Lite, Copyright © 1991-2001, Marshall Cline, Cline@parashift.com)

体,, nicrosoft @ sunistudio.com (East day production room, East day document)

FAQS in section [19]:

[19.1] Is it important for C ? [19.2] When should I inherit? [19.3] How to express inheritance in C ? [19.4] Convert a derived pointer to its base type? [19.5] Public :, private: and protected: What is the difference? [19.6] Why is derived class that cannot access the base class Private: member? [19.7] How can I protect the derivative class when changing the inner part of the class?

[19.1] Is it important for C ?

Yes.

Inheritance is the distinction sign of object-oriented programming and abstract data type (ADT) programming

[TOP | BOTTOM | Previous Section | Next Section]

[19.2] When should I inherit?

As a mechanism for a specialization.

There are two angles of abstract things: "Part" and "Type". Ford Taurus is an (IS-A-Kind-of-A) car, and Ford Taurus has (HAS-A) engine, tire, etc. "Part" level has become part of the software system with the ADT style. Inheritance increases the angle of another decomposition.

[TOP | BOTTOM | Previous Section | Next Section]

[19.3] How to express inheritance in C ?

[Recently Added "Derived Class of" to the list of synonyms (on 7/00). Click Here to Go to the next FAQ in The "chain" of Recent Changes

]

Pass: public syntax:

Class Car: Public Vehicle {public:

// ...

}

We have several ways to state the above relationships:

Car is "a kind of a") Vehicle (vehicle) Vehicle Car is a specialized ("A Specialized") VEHICLE Car is a subclass of Vehicle ("Subclass") Car is a derived class of Vehicle ("Derived Class") Vehicle is the base class of Car ("Base Class") VEHICLE is a super class ("superclass") (this is not commonly used in C communities) )

(Note: The discussion of this FAQ is only related to public inheritance; private and protective inheritance is not the same)

[TOP | BOTTOM | Previous Section | Next Section]

[19.4] Convert a derived pointer to its base type? can. Detective objects are one of the base class objects. Therefore, the conversion from the derived class pointer to the base class pointer is very secure and will always occur. For example, if there is a CAR type pointer, it is actually pointing to Vehicle, which is very secure and conventional conversion from Car * to VEHICLE *:

Void F (VEHICLE * V); Void G (CAR * C) {f (c);}

//

Very secure; no conversion

(Note: The discussion of this FAQ is only related to public inheritance; private and protective inheritance is not the same)

[TOP | BOTTOM | Previous Section | Next Section]

[19.5] Public :, private: and protected: What is the difference?

The members (regardless of the data member or member functions) of the class (regardless of the data member or member function) can only be accessed by the member functions and friend access of the class. The members of the Protected: Section (regardless of the data member or member functions) are only available to the members of the members, friends, and members of the subclavab to be accessed. A member (regardless of the data member or member function) in the PUBLIC: section of the class can be accessed by anyone.

[TOP | BOTTOM | Previous Section | Next Section]

[19.6] Why is derived class that cannot access the base class Private: member?

In order to change the derivation class in the future, it is not affected. Detective classes cannot access private members of the base class. This effectively locked the derivative class when any changes to the base class private member.

[TOP | BOTTOM | Previous Section | Next Section]

[19.7] How can I protect the derivative class when changing the inner part of the class?

[Recently Renamed "Subclass" to "Derived Class" (on 7/00). Click Here to Go To The Next FAQ in The "Chain" of Recent Changes

]

There are two sets of different interfaces, which are respectively for two trivial customers:

Publicated: interface with unrelated class services: interface is a protected: interface

Unless you expect that all of your subclasses are established by your own team, you should consider bringing the base class part into private:, with protected: to access the access function of the base class private data. With this method, the private part can be changed, but the code of the derived class will not be damaged (unless you change the protected access function).

[TOP | BOTTOM | Previous Section | Next Section]

E-mail the author [C FAQ Lite | Table of Contents | Subject Index | About The Author | © | Download Your Own Copy] Revised Apr 8, 2001

转载请注明原文地址:https://www.9cbs.com/read-2039.html

New Post(0)