| < Day Day Up > |
|
The three access specifiers public, protected, and private are used to specify what type of access base class members will have from the derived class when those members are inherited.
Figure 13-3 shows how each access specifier affects the inheritance mechanism.
Figure 13-3: Effects of Using Different Inheritance Specifiers
Use the public access specifier to specify public inheritance. Referring to figure 13-3 above and figure 13-4 below, if the public access specifier is used in the derived class, public data members and functions in the base class are inherited by the derived class and remain public. Protected data members and functions are also inherited by the derived class and remain protected.
Public inheritance is used to implement “is a” relationships between base and derived classes. The use of public inheritance ensures base class interface functions will continue to be inherited if the inheritance hierarchy is extended.
Use the protected access specifier to specify protected inheritance. If protected access is specified, the public and protected data members and functions inherited by the derived class become protected.
Use the private access specifier to specify private inheritance. If private access is specified then the public and protected data members and functions of the base class will become private to the derived class. This effectively prevents any further inheritance of base class data members and functions since private members and functions are not inherited.
Figure 13-4: Public, Protected, & Private Inheritance
Figure 13-5 shows the effects of public inheritance from a horizontal access perspective.
Figure 13-5: Public Inheritance from a Horizontal Access Perspective
The client cannot horizontally access the protected or private members of either the base class or derived class objects.
The access specifiers public, protected, and private are used to control vertical access between base and derived classes and to specify the type of access base class members will have from the derived class. Public inheritance is achieved with the public access specifier. Public inheritance is used to implement an “is a” relationship between a base and derived class. The protected access specifier is used to specify protected inheritance. Protected inheritance is seldom used in practice. Use the private access specifier to specify private inheritance. Private inheritance is used to prevent further implementation of “is a” relationships.
| < Day Day Up > |
|