Saturday, January 30, 2016

INHERITANCE, SINGLE AND MULTIPLE INHERITANCE

Introduction to Inheritance

Reusability is another important feature of OOP. The mechanism of deriving a new class from an old one is called inheritance. Inheritance allows a class to include the members of other classes without repetition of members. There were three ways to inheritance means, “public parts of super class remain public and protected parts of super class remain protected.” Private Inheritance means “Public and Protected Parts of Super Class remain Private in Sub-Class”. Protected Inheritance means “Public and Protected Parts of Superclass remain protected in Subclass”.


CONCEPT OF INHERITANCE

Inheritance is a concept which is the result of commonality between classes. Due to this mechanism, we need not repeat the declaration as well as member functions in a class if they are already present in another class. For example, consider the classes namely “minister” and “prime minister”. Whatever information is present in minister, the same will be present in prime minister also. Apart from that there will be some extra information in class prime minister due to the extra privileges enjoyed by him. Now, due to the mechanism of inheritance, it is enough only to indicate that information which is a specific to prime minister in its class. In addition, the class prime minister will inherit the information of class minister.

BASE CLASS AND DERIVED CLASS


Let us take the classes, Employee and Manager. A Manager is an Employee with some additional information. when we are declaring the classes Employee and Manager without applying the concept of inheritance, they will look as follows:

class Employee

{
 public:
char* name;
int age;

char* address;
int salary; char*department; 
int id;

};

Now, the class Manager is as follows:

Class Manager {
 public:
char* name;

int age;
char* address; 
intsalary; char*department; 
int id;

employee* team_members; //He heads a group of employees int level; // his position in hierarchy of the organisation

.
.
.
.
};

Now, without repeating the entire information of class Employee in class Manager, we can declare the Manager class as follows:

class Manager: Public Employee

public: Employee*Team_members; int level;

.
.
.
.
};

The latest declaration of class Manager is the same as that of its previous one, with the exception that we did not repeat the information of class Employee explicitly. This is what is meant by the Application of inheritance mechanism. Please note that in the above example, Employee is called Base Class and Manager is called Derived Class.

Inheritance Visibility Mode
Depending on Access modifier used while inheritance, the availability of class members of Super class in the sub class changes. It can either be private, protected or public.


1) Public Inheritance
This is the most used inheritance mode. In this the protected member of super class becomes protected members of sub class and public becomes public.
class Subclass : public Superclass


2) Private Inheritance
In private mode, the protected and public members of super class become private members of derived class.
class Subclass : Superclass   // By default its private inheritance


3) Protected Inheritance
In protected mode, the public and protected members of Super class becomes protected members of Sub class.
class subclass : protected Superclass



SINGLE INHERITANCE


In this Section, you will learn the ways of deriving a class from single class. So, there will be only one base class for the derived class.


Private Inheritance

Consider the following classes:

class A { /*......*/); class C: private A

{ /*
.
. .
.
*/
}

All the public parts of class A and all the protected parts of class A, become private members/parts of the derived class C in class C. No private member of class A can be accessed by class C. To do so, you need to write public or private functions in the Base class. A public function can be accessed by any object, however, private function can be used only within the class hierarchy that is class A and class C and friends of these classes in the above cases.



Public Inheritance

Consider the following classes: class A{/*........*/};
class E: public A
{
/*


:


:

};
:




Now, all the public parts of class A become public in class E and protected part of
A become protected in E

Protected Inheritance

Consider the following classes: class E: protected A

{ /*
.
.
.
*/
};

Now, all the public and protected parts of class A become protected in class E.

No private member of class A can be accessed by class E. Let us take a single example to demonstrate the inheritance of public and private type in more details.

Example:

#include <iostream.h>

class Value
{
protected:
int val;
public:
void set_values (int a)
{
val=a;
}
};

 class Cube: public Value
{
public:
int cube()
{
 return (val*val*val);
 }
};
int main ()
{
Cube cub;
cub.set_values (5);
cout << "The Cube of 5 is::" << cub.cube() << endl;
return 0;
}

Output:
The Cube of 5 is:: 125

MULTIPLE INHERITANCE

A class can have more than one direct base classes.

Consider the following classes:

Class A {/*
.....*/};
Class B {/* .....
*/};
Class C : public A, public B

{   /*

.

.
.
.

*/

};

This is called Multiple Inheritance. If a class is having only one base class, then it is known as single inheritance. In the case of Class C, other than the operations specified in it, the union of operations of classes A and B can also be applied.

Example:
#include<iostream.h>
#include<conio.h>

class student
{
    protected:
       int rno,m1,m2;
    public:
                void get()
              {
                            cout<<"Enter the Roll no :";
                            cin>>rno;
                            cout<<"Enter the two marks   :";
                            cin>>m1>>m2;
              }
};
class sports
{
    protected:
       int sm;                   // sm = Sports mark
    public:
                void getsm()
              {
                 cout<<"\nEnter the sports mark :";
                 cin>>sm;

              }
};
class statement:public student,public sports
{
    int tot,avg;
    public:
    void display()
              {
                 tot=(m1+m2+sm);
                 avg=tot/3;
                 cout<<"\n\n\tRoll No    : "<<rno<<"\n\tTotal      : "<<tot;
               cout<<"\n\tAverage    : "<<avg;
              }
};
void main()
{
   clrscr();
   statement obj;
   obj.get();
   obj.getsm();
   obj.display();
   getch();
}

Output:

Enter the Roll no: 100
 Enter two marks
 90
 80
 Enter the Sports Mark: 90
 Roll No: 100
 Total    : 260
Average: 86.66




No comments:

Post a Comment