Yep.
Inheritance is what separates abstract data type (ADT) programming from OO programming.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
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 | Search the FAQ ]
By the : public syntax:
We state the above relationship in several ways:
(Note: this FAQ has to do with public inheritance; private and protected inheritance are different.)
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
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:
(Note: this FAQ has to do with public inheritance; private and protected inheritance are different.)
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
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 | Search the FAQ ]
A class has two distinct interfaces for two distinct sets of clients:
Unless you expect all your derived classes to be built by your own team, you should declare your base class's data members as private and use protected inline access functions by which derived classes will access the private data in the base class. This way the private data declarations can change, but the derived class's code won't break (unless you change the protected access functions).
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
E-mail the author
[ C++ FAQ Lite
| Table of contents
| Subject index
| About the author
| ©
| Download your own copy ]
Revised Aug 15, 2001