Access Specifiers And Vertical Access

 < 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.

click to expand
Figure 13-3: Effects of Using Different Inheritance Specifiers

Public Inheritance

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.

Protected Inheritance

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.

Private Inheritance

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.

click to expand
Figure 13-4: Public, Protected, & Private Inheritance

Figure 13-5 shows the effects of public inheritance from a horizontal access perspective.

click to expand
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.

Quick Review

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 > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net