So far, you have seen only instances of
public derivation, marked
by the symbol public
.
If you change a derivation from public to protected, every public member variable or member function in the base class acts as though it were part of the protected part of the definition of the derived class.
Suppose, for example, that you define the box
and
box_car
classes as follows, with a
protected derivation of
the box_car
class from the box
class:
class box {
public: double height, width, length;
// Default constructor:
box ( ) { }
// Argument-bearing constructor:
box (double h, double w, double l) {
height = h; width = w; length = l;
}
// Volume member function:
double volume ( ) {return height * width * length;}
};
class box_car : public railroad_car, protected box {
public:
// Default constructor:
box_car ( ) : box (10.5, 9.5, 40.0) { }
// Displayers:
virtual void display_height ( ) {cout << height;}
virtual void display_short_name ( ) {cout << "box";}
virtual void display_capacity ( ) {cout << volume ( );}
};
Because the derivation of box_car
, relative to the box
class,
is a protected derivation, all public member variables and member functions
defined in the box
base class act as though they are protected
member variables and member functions in the derived box_car
class.
Because they act as though they are protected, they are still accessible to
member functions defined in the box_car
class and to member
functions defined in any subclasses that may be derived from the
box_car
class. For example, the display_height
function
defined in the box_car
class can read from the height
member
variable.