Inheritance

 

 University Example

 

In university, system players can be student ,professor and administrative staff. They are all people and have common properties .

 

Thus a class Person can be defined containing all properties common to the three , such as name ,address, gender and date of birth.

 

A Student class can inherit from the Person class and adds more details such as number ,major, minor ,GPA and so on.

 

A Professor class can inherit from the Person class and and adds more details that are specific to a professor such as courses he/she teaches , lecture time , office hours  and so on. A similar approach is followed to define a class for administrative class .

 Geometric shapes Example

A rectangle can inherit from line and adds to its extra dimension height. A cube can inherit from a rectangle and adds to its extra dimension depth.

 

The properties (data and functions) that are declared in the Line class are available for the Rectangle class.

 

The function showDim() declared in both classes demonstrates polymorphism and that is the act of overriding an inherited function by a new one. They are preceded by the keyword virtual to enable inheriting (derived) classes to override them , but maintain the same name.

 

Multiple inheritance : A class inheriting from more than one base class .

 Access Specification

  1. Private : only class methods can access private members.

  2. Protected: Allows derived class to access its members.

  3. Public: Allows global access .

The public part acts as an interface through which it presents its services .

 Inheritance Specification and its effect

Base Class Member

Inheritance Specification

 Derived Class Members

Private

Protected

Public

Private

Private

Private

Inaccessible

Private

Private

Private

Protected

Public

Protected

Protected

Protected

Inaccessible

Protected

Protected

Private

Protected

Public

Public

Public

Public

Inaccessible

Protected

Public