Inheritance and Access Control

Index



Base Class Access Control

  • Public
    • Public remain public
    • Private remain private
    • Protected remain protected
  • Protected
    • Protected members are inherited as private members
  • Class derivation access is private by default when using class keyword. This means that if access_specifier is not defined, then it is private by default.
    class DerivedClass:access_specifier base_class{
    };
    
  • In order to make a public derivation of a base class, public should be defined:
    class Car:public Vehicle{
    };
    
  • Also you may use struct keyword to define a class. The funny thing about doing this is that all that is private by default when using class keyword, is public when using struct keyword.
  • Even funnier!, you may use union keyword to define a class :D. This is interesting because only one member of the class can be used at a time, and also as in classic unions, the bigger member will define the size of the class in memory.




    Ways of Inheritance

  • Public Inheritance
    • Specified between two classes as:
      class Car:public Vehicle
    • Base class Private members
      • manipulate through inherited member functions
      • not accessible directly
      • still inherited
    • Base class Public and Protected members
      • inherited with original member access
    • friend functions not inherited
  • Protected Inheritance
    • Specified between two classes as:
      class Car:protected Vehicle
    • Base class Private members
      • same as public inheritance
    • Base class Protected members
      • retain the protected feature in the derived class
    • Base class Public members
      • becomes protected in the derived class
    • friend functions not inherited
  • Private Inheritance
    • Specified between two classes as:
      class Car:private Vehicle
      class Car: Vehicle
    • Base class Private members
      • same as public inheritance
    • Base class Protected and Public members
      • becomes private in the derived class
    • friend functions not inherited




    Access Mechanism in Classes






    Effects of Inheritance on Members Visibility






    Member Access in Base Class

    private protected public

    Always inaccessible regardless of derivation access

    Private in derived class if you use private derivation

    Private in derived class if you use private derivation

    Protected in derived class if you use protected derivation

    Protected in derived class if you use protected derivation

    Protected in derived class if you use public derivation

    Public in derived class if you use public derivation




    Multiple Multilevel Inheritance

  • This is one of my favorite examples of inheritance in C++, this shows how powerful inheritance perspective is in this language for SW design.
  • Consider the case of processing the student results where weighting have to be given for sports before finalizing the results. The weighting for sports is stored in a separate class called sports. The inheritance between the various classes is:






    Inheritance Summary






    Multipath Inheritance Ambiguity