User Menu

Login



Share with

Inheritance PDF Print E-mail

Inheritance in C++

A key feature of C++ is inheritance. Inheritance allows to create classes which are derived from other classes, so that they automatically include some of its "parent's" members, plus its own. For example, If we declare a class FRUIT which describes all the common attribute of fruits such as their taste , colour etc. From this FRUIT class we cab drive two other classes Apple and Orange.

 

 inheri

The class Fruits would contain members that are common for both types of Fruits. In our case: Taste and Colour. And Orange and Apple would be its derived classes, with specific features that are different from one type of fruit to the other.

If a base class includes a member A and we derive it to another class with another member called B, the derived class will contain both members A and B.

In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format:

Class derived_class: access_specifier base_class


{ /*...*/ };

Where derived_class_name is the name of the derived class and base_class_name is the name of the class on which it is based. The access specifier may be replaced by any one of the access specifiers such as public,protected and private. This access specifier limits the most accessible level for the members inherited from the base class: The members with a more accessible level are inherited with this level instead, while the members with an equal or more restrictive access level keep their restrictive level in the derived class.

// Inheritance Example

#include <iostream.h>

#include<string.h>

class Fruits

  {

  public:
    char taste[20],color[20];

  };

class Orange: public Fruits

  {

  public:

    void TasteColor()

      {

       strcpy(taste,"sweet-sour");

       strcpy(color,"orange"); 

       }

    void Display()

       {

       cout<<"Orange Taste = "<<taste<<endl;

       cout<<"Orange Colour ="<<color<<endl; 

       }

  };

class Apple: public Fruits

{
  public:

      void TasteColor()

       {

       strcpy(taste,"sweet");

       strcpy(color,"red"); 

        }

          void Display()

       {

       cout<<"Apple's Taste = "<<taste<<endl;

       cout<<"Apple's Colour ="<<color<<endl; 

       }

  };

int main ()

{
  Orange org;

  Apple  app;

  org.TasteColor();

  app.TasteColor();

  org.Display();

  app.Display();

  return 0;

Output:

Orange Taste = sweet-sour

Orange Colour =orange

Apple Taste = sweet

Apple Colour =red

The objects of the classes Orange and Apple each contain members inherited from Fruits. These are: taste and color().

Since we wanted taste and color to be accessible from members of the derived classes Orange and Apple, we have used public access specifier.

We can summarize the different access types according to who can access them in the following way:

Access 

Public 

Protected

Private

Members of the same class

yes

yes

Yes

Members of derived classes

Yes

Yes

No

Non members 

Yes

No

No


Where "not members" represent any access from outside the class, such as from main(), from another class or from a function.

Access Specifier in Inheritance:

When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier .

public inheritance is commonly used. While using different type of inheritance, following rules are applied:

1.Public Inheritance:

2.Protected Inheritance:

3.Private Inheritance:

In our example, the members inherited by Orange and Apple classes have the same access permissions as they had in their base class Fruits:This is because we have used the public keyword to define the inheritance relationship on each of the derived classes:

class Orange: public Fruits{ ... }


This public keyword after the colon (:) denotes the most accessible level the members inherited from the class that follows it (in this case Orange) will have. Since public is the most accessible level, by specifying this keyword the derived class will inherit all the members with the same levels they had in the base class.

If we specify a more restrictive access level like protected, all public members of the base class are inherited as protected in the derived class. Whereas if we specify the most restricting of all access levels: private, all the base class members are inherited as private.

For example, if we derive Orange class as:
class Orange: protected Fruits;


This would set protected as the maximum access level for the members of Orange that it inherited from Apple. That is, all members that were public in Fruits would become protected in Orange. Of course, this would not restrict Orange to declare its own public members. That maximum access level is only set for the members inherited from Fruits.

If we do not explicitly specify any access level for the inheritance, the compiler assumes private for classes declared with class keyword and public for those declared with struct.

Forms of Inheritance

Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class.

Multiple Inheritance:It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es)

Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherits from one base class.

Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes.

Hybrid Inheritance:The inheritance hierarchy that reflects any legal combination of other four types of inheritance.

Types of Inheritance

Examples

Single Inheritance

When a class is inherited from one base class,this type of Inheritance is called Single Inheritance Example 
// Inheritance Example

#include <iostream.h>

#include<string.h>

class Fruits

  {

  public:
  char taste[20],color[20];

     
  };

class Orange: public Fruits // Single Inheritance

  {

  public:

    void TasteColor()

      {

       strcpy(taste,"sweet-sour");

       strcpy(color,"orange"); 

       }

    void Display()

       {

       cout<<"Orange Taste = "<<taste<<endl;

       cout<<"Orange Colour ="<<color<<endl; 

       }

  };

int main ()

{
  Orange org;

  org.TasteColor();
  org.Display();
  return 0;

Output:

Orange's Taste = sweet-sour

Oange's Colour =orange

Multiple Inheritances:

A C++ class can inherit members from more than one class and here is the syntax:

class derived-class: access base_A, access base_B....

Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above. Let us try the following example:

#include<iostream.h>
#include<conio.h>
     class A
        {
        protected:
        int m;
        };
    class B
        {        
        protected:
        int o;
        };
    class C: public A, public B  //Use of Multiple Inheritance     
    {
       public:

       void set_m_o()

        {

        m=5;  // m is Inherited from class A

        o=6;  // o is Inherited from class B

        } 
       void display() // Not Inherited from any class 
       {
       cout<<"m="<<m<<endl;  
       cout<<"o="<<o<<endl;
       cout<<"m*o="<<m*o<<endl;
       }
    };
    int main()
    {
        A aobj;
        B bonj;
        C cobj;
        cobj.set_m_o();
        cobj.display();
        return 0;
    }
Output:
m=5
o=6
m*o=30

Multi Level Inheritances:

#include<iostream.h>
class A
{

public :

int a1;

};
class B: public A //class B is publicly derived by class A
{

public :

int b1;
};
class C: public B  // //class C is publicly derived by class B 
{

public :
void Cgetdata()
{
cout<<endl<<"Enter the values of a1 and b1";
cin>>a1>>b1; // a1 and b1 are derived from class A and class B 
}
void Cputdata()
{
cout<<endl<<"You have entered a1 = "<<a1<<"and b1 is ="<<b1;
}
};
int main()
{
C obj;
obj.Cgetdata(); //member function of class C
obj.Cputdata(); //member function of class C
return 0


;
}

Output:

Enter the values of a1 and b1

10 20

You have entered a1= 10 b1 = 20

What is inherited from the base class?

In principle, a derived class inherits every member of a base class except:

its constructor and its destructor its operator=() members its friends

Although the constructors and destructors of the base class are not inherited themselves, its default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed.

If the base class has no default constructor or you want that an overloaded constructor is called when a new derived object is created, you can specify it in each constructor definition of the derived class:

derived_constructor_name (parameters) : base_constructor_name (parameters) {...}

Example: 
// constructors and derived classes

#include <iostream.h>

class A

{
  public:


    A()

       {

        cout << "A's no parameter constructor\n";

       }

        A(int a)

      {

       cout << "A's one parameter constructor\n";

      }

};

class B : public A

{
  public:

B(int a) // A's no parameter constructor will called by the compiler

     {

       cout << "B's one parameter constructor\n";

      }

};

class C : public A

{
  public:

C(int a) : A(a)   // Explicit Call to A;s one parameter constructor
      {

       cout << "C's one parameter constructor\n";

      }

};

int main ()

{
  B bobj(0);

  C cobj(0);

  return 0;

}    

A's no parameter constructor

B's one parameter constructor

A's one parameter constructor

C's one parameter constructor

Notice the difference between which A's constructor is called when a new B's object is created and which a new C's object is created. This difference is because of the constructor declaration of B and C class:

B(int a)   // nothing specified: call default by the compiler to A's no parameter constructor

C(int a) : A(a)  // constructor specified: Explicit call to the A's one parameter constructor

scroll back to top