home
C++ TUTORIALS

INHERITANCE:EXTENDING CLASSES

C++ supports reusability by creating new classes,reusing the properties of the existing ones.The mechanism of deriving new classes from an old one is called inheritance.The old one is referred as the base class and the new on is called the derived class or subclass.The derived class inherits some or all properties of the base class.A class can inherit properties from more than one class or levels.

DERIVED CLASS-

It is defined by specifying its relation with the base class in addition to its own details.

	
class derived-class-name : visibility-mode base-class-name { .......// .......// members of derived class .......// };
The ";" indicates that the derived-class-name is derived from the base-class-name.The visibility is optional and can be private or public.The default mode is private.It specifies whether the features of base class are privately derived or publicly derived.
When base class is privately inherited,the public members of the base class become private member of the derived class and hencethe public members of the base class can only be accessed by the member functions of the derived class.No member of the base class is accessible to the objects of the derived class.
When base class is publicly inherited,the pulic members of the base class become the public members of the derived class and they are accissible to the objects of the derived class.
In both cases the private members are not inherited and therefore,the private members of the base class will never become the members of its derived class.

MAKING A PRIVATE MEMBER INHERITABLE-/

Private member can be inherited by modifying the the visibility limit of the private member by making it public. C++ provides a third visibility modifier,protected,which serve a limited purpose in inheritance.A member declared as protected is accessible by the member functions within the class and any class immediately derived from it and not by the functions outside these two classes.

A protected member is inherited in public moode, it becomes protected in the derived class too and therefore is accessible by the member functions of the derived class.A protected member inherited in private mode becomes private in derived class.
A class can be derived in protected mode also.Both the public and protected members of the base class become protected members of the derived class.The following do not have access to the private and protected members-

  1. A function that is a friend of the class.
  2. A member function of a class that is a friend of the class.
  3. A mmber function of a derived class.

There 5 types of inheritances,discussed as follows-