Inheritance in C++
Inheritance In C++
What
is Inheritance in C++?
The capability of a class to derive properties and
characteristics from another class is called Inheritance. Inheritance is one of
the most important feature of Object Oriented Programming.
Ø Sub
Class: The class that inherits properties from another
class is called Sub class or Derived Class.
Ø Super
Class: The class whose properties are inherited by sub
class is called Base Class or Super class.
Inheritance
Example
Suppose there are 3 sections in the 12th grade of
your school: A, B, and C. The functions that we need to perform in each class
are taking the attendance, distributing newspapers to students who subscribed
for it, and giving them their final grades.
Modes
of Inheritance in C++
Ø Public
mode: If we derive a sub class 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 derived
class.
Ø Protected
mode: If we derive a sub class from a Protected base
class. Then both public member and protected members of the base class will
become protected in derived class.
Ø Private
mode: If we derive a sub class from a Private base class.
Then both public member and protected members of the base class will become
Private in derived class.
Types
of Inheritance in C++
There are 5 types of inheritance in C++. The
classification of inheritance is based on how the properties of the base class
are inherited by the derived class(es).
1.
Single Inheritance:
(Single
Inheritance)
Syntax: class subclass_name :
access_mode base_class { //body of subclass };
Code-:
#include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
Output-:
1.
Multiple
Inheritance:
When a class is derived from two or
more base classes, such inheritance is called Multiple Inheritance. It allow us to combine the features of several
existing classes into a single class. We
can also say that in multiple inheritance, a single class is derived from two
or more parent classes. So, there may be a possibility that two or more parents
have same named member function.
|
Code-:
// C++ program to explain
// multiple inheritance
#include <iostream>
using namespace std;
// first base class
class Vehicle
{
public:
Vehicle()
{
cout << " This is a
Vehicle" << endl;
}
};
// second base class
class FourWheeler
{
public:
FourWheeler()
{
cout << " This is a 4
wheeler Vehicle" << endl;
}
};
// sub class derived from two base classes
class Car: public Vehicle, public FourWheeler
{
};
int main()
{
Car obj;
return 0;
}
Output:
3.
Multilevel Inheritance:
In
this type of inheritance, a derived class is created from another derived
class.
(Multilevel
Inheritance in C++)
Syntax: class A // base class {
........... }; class B : acess_specifier A // derived
class {
........... } ; class C : access_specifier B // derived
from derived class B {
........... } ;
Code-:
#include <iostream>
using namespace std;
class A {
public:
void
display() {
cout<<"\nBase class content.";
}
};
class B : public A {};
class C : public B {};
int main() {
C obj;
obj.display();
return 0;
}
Output-:
4. Hierarchical
inheritance:
Hierarchical inheritance is a kind
of inheritance where more than one class is inherited from a single parent or
base class. Especially those features which are common in the parent class is
also common with the base class. According to the syntax, all the common
features in the parent class get extracted or inherited by the child class and
the methods in the child class vice-versa also happens. Therefore, it can be
concluded that n-number of child class or base class can inherit the properties
of parent class and the vice-versa can also happen. Also, it is not necessary
that only common features can be inherited. Any other feature can also be
inherited.
Real-life examples:
1.Programming languages are derived
from languages.
2.Smart tv, LED TV all these Tv
series are derived from normal youtube tv sets
Syntax: class A //
Base class of B
{
…
};
class B
: access_specifier A // Derived class of A
{
…
};
class C
: access_specifier A // Derived class of A
{
…
};
class D
: access_specifier A // Derived class of A
{
…
};
Code:
#include <iostream>
using namespace std;
class Shape // Declaration of base class.
{
public:
int a;
int b;
void get_data(int n,int m)
{
a= n;
b = m;
}
};
class Rectangle : public Shape // inheriting Shape class
{
public:
int rect_area()
{
int result = a*b;
return result;
}
};
class Triangle : public Shape // inheriting Shape class
{
public:
int triangle_area()
{
float result = 0.5*a*b;
return result;
}
};
int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
std::cout << "Enter the length and breadth of a rectangle: " << std::endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
std::cout << "Area of the rectangle is : " <<m<< std::endl;
std::cout << "Enter the base and height of the triangle: " << std::endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
std::cout <<"Area of the triangle is : " << n<<std::endl;
return 0;
}
5. Hybrid
(Virtual) Inheritance:
Hybrid
Inheritance is implemented by combining more than one type of inheritance. For
example: Combining Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of
hierarchical and multiple inheritance:
Code:
#include
<iostream>
using
namespace std;
class
Vehicle
{
public:
Vehicle()
{
cout << "This is a
Vehicle" << endl;
}
};
class
Fare
{
public:
Fare()
{
cout<<"Fare of
Vehicle\n";
}
};
class
Car: public Vehicle
{
};
class
Bus: public Vehicle, public Fare
{
};
int
main()
{
Bus obj2;
return 0;
}
Output:-





Comments
Post a Comment