Thursday, January 31, 2013

Inheritance - C++





Inheritance is an object-oriented programming principle. It is the ability  of a subclass to take  the characteristics from other class on which it's based on is known as Inheritance. . In inheritance the subclass is known as derived class or child class which derived from a parent class which also known as base class or Super class. In c++ we get are six different forms of inheritance.
Objects those declared as public  share with all other classes. Where  Private Identifiers and member methods only accessed by the member of the class and inner classes. protected data and member methods meant for the derived classes 
The derived class can share the members of the base class as well as the new members of its own. Therefore, a derived class can use members of the base class, unless same members are redefined or declared as private in base class. The base class should be written before the derived class.

1.           Single inheritance : Only one Base or Super Class
2.           Multiple inheritance : A subclass can inherit several Base classes
3.           Hierarchical inheritance : Many subclass with only one super class
4.           Multi-Level inheritance : Derived classed derived from other derive classes
5.           Hybrid inheritance : When more than two above mentioned type of inheritance used is known as Hybrid Inheritance.
6.           Multi-path inheritance : Get some or all properties from two sources.



Single Inheritance : In single inheritance classes have only one base class. B_Class is derived from A_Class.





Example :

  class A_Class
      {
           protected:
              int Num;
      };

  class B_Class : public A_Class
      {
            private:
              int Num1;
      };





Multiple inheritance : A subclass can inherit several Base classes. C_Class is derived from both A_Class and B_Class.





Example :

  class A_Class
      {
           protected:
              int Num;
      };
  class B_Class
      {
           protected:
              int Num1;
      };

  class C_Class : public A_Class,B_Class
      {
            private:
              int Num2;
      };




Hierarchical inheritance : Many subclass with only one super class. Three classes, B_Class, C_Class and D_Class derived from base class A_Class.





Example:

  class A_Class
      {
           protected:
              int Num;
      };

  class B_Class : public A_Class
      {
            private:
              int Num1;
      };


  class C_Class : public A_Class
      {
            private:
              int Num2;
      };
  class D_Class : public A_Class
      {
            private:
              int Num3;
      };



Multi-Level inheritance : Derived classes derived from other derive classes. C_Class derived from B_Class and which already derived from A_Class.





Example:

  class A_Class
      {
           protected:
              int Num;
      };

  class B_Class : public A_Class
      {
            protected:
              int Num1;
      };


  class C_Class : public B_Class
      {
            private:
              int Num2;
      };




Hybrid inheritance : When more than two above mentioned type of inheritance used is known as Hybrid Inheritance. Here applying both types, Multiple and Multilevel inheritance. Where C_Class derived from both A_Class and B_Class and whereas D_Class derived from C_Class.





Example:

  class A_Class
      {
           protected:
              int Num;
      };

  class B_Class
      {
            protected:
              int Num1;
      };


  class C_Class : public A_Class, B_Class
      {
            protected:
              int Num2;
      };
  class D_Class : public C_Class
      {
            private:
              int Num3;
      };



Multi-path inheritance : Share some or all properties from two sources. Multi-path inheritance is a type when a class is derived from more than one classes and those classes are derived  from same base class.






Example:

  class A_Class
      {
           protected:
              int Num;
      };
  class B_Class : public A_Class
      {
           protected:
              int Num1;
      };

  class C_Class : public A_Class
      {
            private:
              int Num2;
      };

  class D_Class : public B_Class, C_Class
      {
            private:
              int Num3;
      };










No comments: