2. Introduction of inheritance:
One of the most important concepts in object-oriented
programming is that of inheritance. Inheritance allows us to
define a class in terms of another class, which makes it easier to
create and maintain an application. This also provides an
opportunity to reuse the code functionality and fast
implementation time.
When creating a class, instead of writing completely new data
members and member functions, the programmer can designate
that the new class should inherit the members of an existing
class. This existing class is called the base class, and the new
class is referred to as the derived class.
3. Definition
The Capability of a class to derive properties and characteristics
from another class is called inheritance.
The derived class inherits all the features from the base class
and can have additional features of its own.
Inheritance is one of the most important feature of Object
Oriented Programming.
4. Sample Example
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
5. Classifications:
In C++, We have 5 different types of Inheritance. Namely
Single Inheritance
Multilevel Inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance (also known as Virtual Inheritance)
6. Classifications:
In C++, We have another types of Inheritance. Namely
Multipath Inheritance(It’s a part of inheritance,and it’s use as
ambiguity)
7. Single inheritance
In this inheritance, a derived class is created from
a single base class.
Class A
Class B
8. Sample Program
//Base Class
Class A {
public void fooA() {
//TO DO:
}
}
//Derived Class
Class B : A {
public void fooB() {
//TO DO:
}
}
10. Sample Program
//Base Class
class A {
public void fooA() {
//TO DO:
}
}
//Derived Class
class B : A {
public void fooB() {
//TO DO:
}
}
//Derived Class
class C : B {
public void fooC() {
//TO DO:
}
}
11. Multiple inheritance
In this inheritance, a derived class is created from
more than one base class. This inheritance is not
supported by .NET Languages like C#, F# etc.
Class A Class B
Class C
12. Sample Program
//Base Class
class A {
public void fooA() {
//TO DO:
}
}
//Derived Class
class B {
public void fooB() {
//TO DO:
}
}
//Derived Class
class C : A, B {
public void fooC() {
//TO DO:
}
}
13. Hierarchical inheritance
In this inheritance, more than one derived classes are
created from a single base.
Class C Class B
Class D
Class A
Class E Class F Class G
14. Sample Program
//Base Class
class A {
public void fooA() {
//TO DO:
}
}
//Derived Class
class B : A {
public void fooB() {
//TO DO:
}
}
//Derived Class
class C : A {
public void fooC() {
//TO DO:
}
}
//Derived Class
class D : C {
public void fooD() {
//TO DO:
}
}
15. Sample Program
//Base Class
class E:C {
public void fooE() {
//TO DO:
}
}
//Derived Class
class F : B {
public void fooF() {
//TO DO:
}
}
//Derived Class
class G: B {
public void fooG() {
//TO DO:
}
}
16. Hybrid inheritance
This is combination of more than one inheritance. Hence, it
may be a combination of Multilevel and Multiple inheritance
or Hierarchical and Multilevel inheritance or Hierarchical
and Multipath inheritance or Hierarchical, Multilevel and
Multiple inheritance.
18. Sample Program
//Base Class
class A {
public void fooA() {
//TO DO:
}
}
//Derived Class
class F {
public void fooF() {
//TO DO:
}
}
//Derived Class
class B: A, F{
public void fooB() {
//TO DO:
}
}
//Derived Class
class C : A{
public void fooC() {
//TO DO:
}
}
19. Sample Program
//Base Class
class D: C {
public void fooD() {
//TO DO:
}
}
//Derived Class
class E : C{
public void fooE() {
//TO DO:
}
}
20. Multipath inheritance
In this inheritance, a derived class is created from another
derived classes and the same base class of another derived
classes. This inheritance is not supported by .NET Languages
like C#, F# etc.
Class D Class B
Class C
Class A
21. Multipath inheritance
//Base Class
class A {
public void fooA() {
//TO DO:
}
}
//Derived Class
class B : A {
public void fooB() {
//TO DO:
}
}
//Derived Class
class C : A {
public void fooC() {
//TO DO:
}
}
//Derived Class
class D : B, A, C {
public void fooD() {
//TO DO:
}
}
22. If you need any kind of information
just contact me.
E-mail: redwanislamdip@gmail.com