FAQs in section [19]:
[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++? 
[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.]
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 a "subclass" of Vehicle
- Car is a "derived class" 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 derived classes from breaking when I
change internal parts? 
[Recently renamed "subclass" to "derived class" (on 7/00). Click here to go to the next FAQ in the "chain" of recent changes.]
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 derived classes 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 Jul 10, 2000
|