[19] Inheritance basics, C++ FAQ Lite
[19] Inheritance basics
(Part of C++ FAQ Lite, Copyright © 1991-98, Marshall Cline, cline@parashift.com)
FAQs in section [19]:
[19.1] Is inheritance important to C++?
[19.2] When would I use inheritance?
[19.3] How do you express inheritance in C++?
[19.4] Is it OK to convert a pointer from a derived class
to its base class?
[19.5] What's the difference between public:, private:, and
protected:?
[19.6] Why can't my derived class access private: things
from my base class?
[19.7] How can I protect subclasses from breaking when I
change internal parts?
[19.1] Is inheritance important to C++?
Yep.
Inheritance is what separates abstract data type (ADT) programming from OO
programming.
[ Top | Bottom | Previous section | Next section ]
[19.2] When would I use inheritance?
As a specification device.
Human beings abstract things on two dimensions: part-of and kind-of. A Ford
Taurus is-a-kind-of-a Car, and a Ford Taurus has-a Engine, Tires, etc. The
part-of hierarchy has been a part of software since the ADT style became
relevant; inheritance adds "the other" major dimension of decomposition.
[ Top | Bottom | Previous section | Next section ]
[19.3] How do you express inheritance in C++?
By the : public syntax:
class Car : public Vehicle {
public:
// ...
};
We state the above relationship in several ways:
Car is "a kind of a" Vehicle
Car is "derived from" Vehicle
Car is "a specialized" Vehicle
Car is the "subclass" of Vehicle
Vehicle is the "base class" of Car
Vehicle is the "superclass" of Car (this not as common in the
C++ community)
(Note: this FAQ has to do with public inheritance; private and
protected inheritance are different.)
[ Top | Bottom | Previous section | Next section ]
[19.4] Is it OK to convert a pointer from a derived class
to its base class?
Yes.
An object of a derived class is a kind of the base class. Therefore the
conversion from a derived class pointer to a base class pointer is
perfectly safe, and happens all the time. For example, if I am pointing at a
car, I am in fact pointing at a vehicle, so converting a Car* to a Vehicle*
is perfectly safe and normal:
void f(Vehicle* v);
void g(Car* c) { f(c); } // Perfectly safe; no cast
(Note: this FAQ has to do with public inheritance; private and
protected inheritance are different.)
[ Top | Bottom | Previous section | Next section ]
[19.5] What's the difference between public:, private:, and
protected:?
A member (either data member or member function) declared in a
private: section of a class can only be accessed by member functions and
friends of that class
A member (either data member or member function) declared in a
protected: section of a class can only be accessed by member functions and
friends of that class, and by member functions and
friends of derived classes
A member (either data member or member function) declared in a
public: section of a class can be accessed by anyone
[ Top | Bottom | Previous section | Next section ]
[19.6] Why can't my derived class access private: things
from my base class?
To protect you from future changes to the base class.
Derived classes do not get access to private members of a base class.
This effectively "seals off" the derived class from any changes made to the
private members of the base class.
[ Top | Bottom | Previous section | Next section ]
[19.7] How can I protect subclasses from breaking when I
change internal parts?
A class has two distinct interfaces for two distinct sets of clients:
It has a public: interface that serves unrelated
classes
It has a protected: interface that serves derived
classes
Unless you expect all your subclasses to be built by your own team, you should
consider making your base class's bits be private:, and use protected:
inline access functions by which derived classes will access the private
data in the base class. This way the private bits can change, but the
derived class's code won't break unless you change the protected access
functions.
[ 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 May 27, 1998
Wyszukiwarka
Podobne podstrony:
SHSpec 038 6108C11 Basics of Auditing Matter of FactnessSHSpec 188 6208C21 Basics of AuditingSHSpec 046 6108C29 Basics of Auditing2 Basics of Fiber OpticsSHSpec 044 6108C23 Basics of Auditing(eBook TXT) Health Basics of Health, Nutrition and FitnessSHSpec 103 6201C23 Basics of Auditing[dcpp][Bidemare][Costruzione][Diapo][Eng] Fundamentals of Marine Propulsion Steam power basicsDan04 The Abomination of Desolation Understanding the Basics Spring 2015 KDBasicSliderUI FocusHandlerWilliam Gibson Fragments Of A Hologram Roseeffect of varying doses of caffeine on life span D melanogasterThrilling Tales Advanced Class Man of MysteryFunctional Origins of Religious Concepts Ontological and Strategic Selection in Evolved MindsBeyerl P The Symbols And Magick of TarotBeats of freedomNext of Kinwięcej podobnych podstron