Inheritance in C++





Inheritance in C++

Inheritance is one of the most important features of object oriented programming. It is the process of creating new classes from an existing class. The existing class is known as the basic class and the newly created class is called as derived class. The derived class inherits all properties of the base class but can add properties of its own.

                The main advantages of the inheritance are:-

  • It permits code reusability. Once a base class is written and debugged, it can be used in various situations without redefining and rewriting it.
  • Increases the reliability of the code:- Reusing existing code saves time, money and effort and increase programs reliability.
  • Ease of distributing class libraries:- A programmers can use a class created by another person or company and without modifying it, derive other classes from it that are suited to particular situations. Hence, reusability results in the ease of distributing class libraries.

Single inheritance

If a class is derived from only one base class, then it is called single inheritance.

The general syntax of defining a derived class using single inheritance is given below:-

 

Class derived_class_name: visibility_mode base_class_name

{

:::::::::::::: // member of derived class

};

 

Here, the visibility_mode may be either private or public or protected. If no visibility_mode is specified, then by default the visibility_mode is considered as private.

 

E.g.

 

1. Develop an object oriented program to create a database of the student’s information system containing the following information.

I.            Name

II.            Roll number

III.            Address

IV.            Gender

Design a base class with name, roll number and another derived class consisting of other data members.

 

#include<iostream.h>

#include<conio.h>

Class student

{

Private:

Char name [25];

Int roll;

Public:

Void getdata ()

{

Cout<<”\n enter name”;

Cin>>name;

Cout<<”\n enter roll”;

Cin>>roll;

}

Void display ()

{

Cout<<”\n Name=”<<name;

Cout<<”\n Roll no=”<<roll;

}

};

Class stu.address: public student

{

Private:

Char address [25];

Char gender [25];

Public:

Void getdata ()

{

Student:: getdata ()

Cout<<”\n Enter address”;

Cin>>address;

Cout<<”\n enter gender”;

Cin>>gender;

}

Void display ()

{

Student:: display ();

Cout<<”\n Address=”<<address;

Cout<<”\n Gender=”<<gender;

}

};

Void main ()

{

Stu.address a;

a.getdata ();

a. display ();

getch ();

}

 

 

Multiple inheritances in C++


If a class is derived from more than one base class, then it is called multiple inheritances.
The general syntax of defining a derived class using multiple inheritances is:-

 

Class derived_class_name: visibility_mode base 1, visibility_mode base 2

{

……… // member of derived class

};

 

 

Difference between single and multiple inheritances

 

In a single inheritance, a derived class has only a single base. Hence, it can inherit from only a class not from more than one base class. In a single inheritance hierarchy, a derived class typically represents a specialization of its base class.

But, in multiple inheritances, a derived class has multiple base classes. In a multiple inheritance hierarchy, a derived class typically represents a combination of its base class.

 

Advantages of multiple inheritances over single inheritances

In a multiple inheritances, a derived class has multiple base classes whereas in a single inheritance, a derived class has only a single base class. Hence, following are the advantages of multiple inheritances over single inheritance:-

  1. Multiple inheritances can combine the behavior of multiple base classes in a single derived class.
  2. It allows combining the information from more than one base class and helps to extend the possibility to reuse the existing classes.
  3. It has rich semantics and has the ability of directly express complex structure.

 

Disadvantage of multiple inheritances

 

  1. It increases the complexity and conflicts can arise between inherited attributes and methods.

 

A  program to illustrate how a multiple inheritance can be declared and defined in a program.

This program consists of two base classes and one derived class. The base class “Stuinfo” contains the data members: name, roll. And another base class “Stuacademic info” contains the data members: course and semester. The derived class “stuaddressinfo” contains the data members: address only.

 

#include<iostream.h>

#include<conio.h>

Class Stuinfo

{

Private:

Char name [25];

Int roll;

Public:

Void getdata ()

{

Cout<<”\n Enter name”;

Cin>>name;

Cout<<”\n Enter roll”;

Cin>> roll;

}

Void display ()

{

Cout<<”\n name=”<<name;

Cout<<”\n Roll number”<<roll;

}

};

Class Stuacademicinfo

{

Private:

Char course [25];

Char semester [15];

Public:

Void getdata ()

{

Cout<<”\n Enter course name”;

Cin>>course;

Cout<<”\n Enter semester”;

Cin>>semester;

}

Void display ()

{

Cout<<”\n Course “<<course;

Cout<<”\n semester”<<semester;

};

Class stuaddressinfo: public Stuinfo, public Stuacademicinfo

{

Private:

Char address [25];

Public:

Void getdata ()

{

Stuinfo:: getdata ();

Stuacademicinfo:: getdata ();

Cout<<”\n Enter the address”;

Cin>>address;

}

Void display ()

{

Stuinfo:: display ();

Stuacademic:: display ();

Cout <<”\n Address”<<address;

};

Void main ()

{

Stuaddressinfo obj;

Obj .getdata ();

Obj.dispay ();

getch ();

}

 



Related posts:

  1. Defining Member Function In C++ Class methods definitions  (Defining the member functions) Member functions can...
  2. Structure in C++ Structure in C++ Structure is a collection of simple variables...
  3. Array of structure Array of structure It is well known that array is...
  4. Array Of Class Object Array of class objects Array is a collection of similar...
  5. Arrays In C++ Arrays in C++ Array is a collection of similar type...