INHERITANCE

What Are INHERITANCE?

The capacity of a class to get properties and qualities from another class is called Inheritance. Inheritance is a standout amongst the most critical component of Object Oriented Programming. 

Sub Class: The class that acquires properties from another class is called Subclass or Derived Class. 


Super Class: The class whose properties are acquired by subclass is called Base Class or Superclass.



Modes of Inheritance:


Public mode: If we derive a subclass from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in the derived class.



Protected mode: If we derive a subclass from a Protected base class. Then both public member and protected members of the base class will become protected in the derived class.




Private mode: If we derive a subclass from a Private base class. Then both public member and protected members of the base class will become Private in the derived class.



TYPES OF INHERITANCE:


1. Single Inheritance: In single inheritance, a class is allowed to inherit from only one class. i.e. one subclass is inherited by one base class only.

//EXAMPLE

#include<iostream>
using namespace std;
class Base
{public:
void func()
{
cout<<“Base Class Function.”<<endl;
}};

class Derv:public Base
{
public:
void func(
{
cout<<“Derived Class Function.”<<endl;
}};
int main()
{
Derv d;
d.func();
d.Base::func();
return 0;
}
OUTPUT:
Derived Class Function.
Base Class Function.





2Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one subclass is inherited from more than one base classes.

//EXAMPLE
#include<iostream>
#include<string>
using namespace std;
class WingedAnimal
{protected:
int wing_span;
string wing_type;
public:
WingedAnimal(int ws, string wt)
{
wing_span = ws;
wing_type = wt;
}};
class Mammal
{protected:
string syngamy_type;
public:
Mammal(string st)
{
syngamy_type = st;
}};
class Bat:public WingedAnimal, public Mammal
{
public:
Bat(int ws, string wt, string st):WingedAnimal(ws,wt),Mammal(st)
{}
void display()
{
cout<<“Wing Span : “<<wing_span<<” Feet”<<endl;
cout<<“Wing Type : “<<wing_type<<endl;
cout<<“Syngamy Type : “<<syngamy_type<<endl;
}};
int main()
{
Bat b(5,”Elliptical”,”Internal”);
b.display();
return 0;}

OUTPUT:
Wing Span: 5 Feet
Wing Type: Elliptical
Syngamy Type: Internal




3. Multilevel Inheritance: In this type of inheritance, a derived class is created from another derived class.

//EXAMPLE
#include<iostream>
using namespace std;
class student
{protected:
int rollno;
public:
student(int r)
{rollno=r;
}};
 class test:public student
{
protected:
int marks1;
int marks2;
public:
test(int r,int m1,int m2):student(r)
{
marks1=m1; marks2=m2;
}
};
class result:public test
{
int total;
public:
result(int r,int m1,int m2):test(r,m1,m2)
{}
void display()
{
total=marks1+marks2;
cout<<“Roll no : “<<rollno<<endl;
cout<<“Subject 1 : “<<marks1<<endl;
cout<<“Subject 2 : “<<marks2<<endl;
cout<<“Total marks : “<<total<<endl;
}
};
int main()
{
result a(1408,90,80);
a.display();
return 0;
}
OUTPUT:
Roll no : 1408
Subject 1 : 90
Subject 2 : 80
Total marks : 170




4.Hierarchical Inheritance: In this type of inheritance, more than one subclass is inherited from a single base class. i.e. more than one derived class is created from a single base class

//EXAMPLE
#include<iostream>
#include<string>
using namespace std;
class Employee
{
protected:
int id;
string name;
public:
Employee(int i, string n)
{
id=i;
name=n;}};
 class Manager:public Employee
{
int dept_id;
public:
Manager(int i, string n, int d_id):Employee(i,n)
{
dept_id=d_id;
}
void display()
{
cout<<endl<<“Employee ID : “<<id<<endl;
cout<<“Name : “<<name<<endl;
cout<<“Department ID : “<<dept_id<<endl;
}};
 class Scientist:public Employee
{
string specialization;
public:
Scientist(int i, string n, string s):Employee(i,n)
{
specialization = s;
}
void display()
{
cout<<endl<<“Employee ID : “<<id<<endl;
cout<<“Name : “<<name<<endl;
cout<<“Specialization : “<<specialization<<endl;
}};
class Laborer:public Employee
{
int contract_id;
public:
Laborer(int i, string n, int c_id):Employee(i,n)
{
contract_id = c_id;
}
void display()
{
cout<<endl<<“Employee ID : “<<id<<endl;
cout<<“Name : “<<name<<endl;
cout<<“Contract ID : “<<contract_id<<endl;
}
};
int main()
{
Manager m(1235,”Goku”,1001);
Scientist s(1234,”Vegito”,”Quantum Physics”);
Laborer l(1236,”Gohan”,1112);
m.display();
s.display();
l.display();
return 0;
}
OUTPUT:
Employee ID : 1235
Name : Goku
Department ID : 1001


Employee ID : 1234
Name : Vegito
Specialization : Quantum Physics

Employee ID : 1236
Name : Gohan
Contract ID : 1112



5. Hybrid or Virtual Inheritance: Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.

//EXAMPLE
#include<iostream>
using namespace std;
class student
{
protected:
int rollno;
public:
student(int r)
{
rollno=r;
}};
class test:public student
{
protected:
int marks1;
int marks2;
public:
test(int r,int m1,int m2):student(r)
{
marks1=m1; marks2=m2;
}};
class sports
{
protected:
int score;
public:
sports(int s)
{
score = s;
}};
class result:public test,public sports
{
int total;
public:
result(int r, int m1, int m2, int s):test(r,m1,m2),sports(s)
{}
void display()
{
total=marks1+marks2+score;
cout<<“Roll Number : “<<rollno<<endl;
cout<<“Subject 1 : “<<marks1<<endl;
cout<<“Subject 2 : “<<marks2<<endl;
cout<<“Score : “<<score<<endl;
cout<<“Total : “<<total<<endl;
}};
int main()
{
result r(1408,80,90,75);
r.display();
return 0;
}
OUTPUT:
Roll Number : 1408
Subject 1 : 80
Subject 2 : 90
Score : 75
Total : 245

2 Comments