Sunday, 8 April 2018

Types of Inheritance

1. Single level Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance

Single level Inheritance
  A derived class with only one base class is called as single level inheritance.


Here 'A' is superclass or base class.From the class 'A' , the class 'B' is derived so class 'B' is called as subclass of class 'A'.
Identify the type of inheritance and implement it by writing a program for the following figure. Assume suitable member function.



#include<conio.h>
#include<iostream.h>
class student
{
int roll_no;
char name[20];
public:
void accept( )
{
cout<<"\n\tEnter Roll No of Student";
cin>>roll_no;
cout<<"\n\tEnter Name of the student";
cin>>name;
}
void display( )
{
cout<<"\n\tRollNo is--->"<<roll_no;
cout<<"\n\tName is--->"<<name;
}
};
class Eng_student : public student
{
char branch[20];
public:
void accept1( )
{
cout<<"\n\tEnter Branch of student";
cin>>branch;
}
void display( )
{
cout<<"\n\tBranch is--->"<<branch;
}
};
void main( )
{
Eng_student est;
clrscr( );
est.accept( );
est.accept1( );
est.display( );
est.display1( );
getch( );
}

Output:-
Enter Roll NO of Student 101
Enter Name of the student Irawen
Enter Branch of student CS

RollNO is--->101
Name is--->Irawen
Branch is--->CS 


Multilevel Inheritance
   
  The mechanism of deriving a new class from another "derived class" is known as multilevel Inheritance.


The above figure shows multilevel inheritance. The class A serves as a base class for the derived class B which in turn serves as a base class for the derived class C.

The class B is known as inheritance base class since it provides a link for the inheritance between A and C.

Multilevel Inheritance is declared as
Class A
{

};

Class B :public A
{

};

Class C :public B
{

};

The derived class after inheritance would contain its own data members and member function plus members from inheritance base class in addition with members from base class.


Ex:

#include<conio.h>
#include<iostream.h>
class student
{
protected:
int rollno;
public:
void getno(int a);
void putno( );
};
void student::getno(int a)
{
rollno = a;
}
void student::putno( )
{
cout<<"\n\tRollno is--->"<<rollno;
}
class test : public student
{
protected:
float sub1;
float sub2;
public:
void getmarks(float s1, float s2)
{
sub1 = s1;
sub2 = s2;
 }
void putmarks( );
{
cout<<"\n\tMarks of subject1--->"<<sub1<<"\n";
cout<<"\n\tMarks of subject2--->"<<sub2<<"\n";
}
};
class result : public test
{
float total;
public:
void display( )
{
total = sub1 + sub2;
putno( );
putmarks( );
cout<<"\n\tTotal Marks is--->"<<total<<"\n";
}
};
void main( )
{
result st1;
clrscr( );
st1.getno(101);
st1.getmarks(78,56);
st1.display( );
getch( );
}

Output:-
Rollno is--->101
Marks of subject--->78

Marks of subject2--->56

Total Marks is--->134


Multiple Inheritance

  A derived class with the several base class is class is called as multiple inheritance.



Multiple inheritance allows us to combine the features of several existing classes such as starting point for defining new classes.

Syntax:-
Class D : visibility B1, visibility B2 ................visibility Bn
{
//Body of D
};

Ex:
Class B1
{
//Body of B1;
};
Class B2
{
//Body of B2;
};
Class D : public B1 , public B2
{
 //Body of D
}; 

Write a program to implement multiple inheritance

#include<conio.h>
#include<iostream.h>
class base1
{
protected:
int m;
public:
void getm(int m1)
{
m = m1;
}
};
class base2
{
protected :
int n;
public:
void getn(int n1)
{
n = n1;
}
};
class derived:public base1,public base2
{
public:
void display( )
{
cout<<"\n\tvalue of m is--->"<<m;
cout<<"\n\tvalue of n is--->"<<n;
cout<<"\n\tvalue of (m*n) is--->"<<(m*n);
}
};
void main( )
{
derived d;
clrscr( );
d.getm(10);
d.getn(20);
d.display( );
getch( );
}

Output:-
value of m is--->10
value of n is--->20
value of (m*n) is--->200


Hierarchical Inheritance

  When one class may be inherited by more than one class class is known as hierarchical inheritance.

Programming problems can be cast into a hierarchy where certain features of one level are shared by many others below that level.



Here class 'B' and 'C' are derived from class 'A'. that is 'A' is a super class for both the class 'B' and 'C'.

Write a program to implement inheritance shown below. Assume suitable member function.



#include<conio.h>
#include<iostream.h>
class president
{
protected:
char name_p[15];
public:
void accept( )
{
cout<<"\n\tEnter Name of the president";
cin>>name_p;
}
void display( )
{
cout<<"\n\tName of the President--->"<<name_p;
}
};
class vice_president : public president
{
protected:
char name_vp[20];
public:
void accept1( )
{
cout<<"\n\tEnter Name of the vice president";
cin>>name_vp;
}
void display( )
{
cout<<"\n\tName of the vice president is--->"<<name_vp;
}
};
class secretary : public president
{
protected:
char name_s[20];
public:
void accept2( )
{
cout<<"\n\tEnter Name of the secreatry is--->"<<name_s;
}
};
void main( )
{
clrscr( );
vice_president vp;
vp.accept( );
vp.accept1( );
secretary s;
s.accept( );
vp.display( );
vp.display1( );
s.display2( );
getch( );
}

Output:-
Enter Name of the president Irawen
Enter Name of the vice president Pirawen
Enter Name of the secreatry Ntirawen

Name of the President---> Irawen
Name of the vice president--->Pirawen
Name of the secreatry is--->Ntirawen



Hybrid Inheritance

When more than one type of the inheritance is used in any design , it is called as hybrid inheritance

There could be situation where we need to apply two or more types of inheritance to design a problem.
Ex:


Consider the case of processing the students result, we have to give weightage for sports before finishing the result. Weightage for sports before finishing the result. Weightage for sports is stored on separate class called as sports.

The new inheritance relationship between various classes would be as follows.


Write a program to implement hybrid inheritance

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void getno(int a)
{
rollno = a;
}
void putno( )
{
cout<<"\n\n\tRoll No is--->"<<rollno;
}
};
class test : public student
{
protected:
float part1,part2;
public:
void getmarks(float x, float y)
{
part1 = x;
part2 = y;
}
void putmarks( )
{
cout<<"\n\n\tMarks Obtained:"<<part1<<"\t"<<part2;
}
};
class spots
{
protected:
float score;
public:
void getscore(float s)
{
score s;
}
void putscore( )
{
cout<<"\n\n\tSport Weightage is--->"<<score;
}
};
class result : public test,public sports
{
float total;
public:
void display( )
{
total = part1 + part2 + score;
putno( )
putmarks( );
putscore( );
cout<<"\n\n\tTotal Score is--->"<<total<<"\n";
}
};
void main(  )
{
result s1;
clrscr( );
s1.getno(101);
s1.getmarks(78.12,76.23)
s1.getscore(10.0)
s1.display( );
getch( );
}

Output:-
Roll NO is--->101

Marks Obtained :78.120083      76.23003

Sports Weightage is--->10

Total Score is--->164.350006

2 comments:

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (83) R (68) Selenium Webdriver (2) Software (13) SQL (27)