Types of Inheritance

 C++ supports five types of inheritance:

1. Single inheritance

2. Multiple inheritance

3. Hierarchical inheritance

4. Multilevel inheritance

5. Hybrid inheritance


Syntax of Derived class:

class derived_class_name :: visibility-mode base_class_name  

{  

    // body of the derived class.  

}  


Where,

derived_class_name: It is the name of the derived class.

visibility mode: The visibility mode specifies whether the features of the base class are publicly inherited or privately inherited. It can be public or private.

base_class_name: It is the name of the base class.

  • When the base class is privately inherited by the derived class, public members of the base class becomes the private members of the derived class. Therefore, the public members of the base class are not accessible by the objects of the derived class only by the member functions of the derived class.
  • When the base class is publicly inherited by the derived class, public members of the base class also become the public members of the derived class. Therefore, the public members of the base class are accessible by the objects of the derived class as well as by the member functions of the base class.

Note:

  • In C++, the default mode of visibility is private.
  • The private members of the base class are never inherited.

 


Visibility modes can be classified into three categories:

  • Public: When the member is declared as public, it is accessible to all the functions of the program.
  • Private: When the member is declared as private, it is accessible within the class only.
  • Protected: When the member is declared as protected, it is accessible within its own class as well as the class immediately derived from it.

 

No comments:

Post a Comment