Purpose of Inheritance in C++
Purpose of 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 makes the code reusable. When we inherit an existing class, all its methods and
fields become available in the new class, hence code is reused.
1. Code Reusability
2. Method Overriding (Hence, Runtime Polymorphism.)
3. Use of Virtual Keyword
While defining a subclass like this, the super class must be already defined or atleast declared
before the subclass declaration.
Access Mode is used to specify, the mode in which the properties of superclass will be
inherited into subclass, public, privtate or protected.
Example of Inheritance
Whenever we want to use something from an existing class in a new class, we can use the
concept on Inheritace. Here is a simple example,
class Animal
{
public:
int legs = 4;
};
int main()
{
Dog d;
cout << d.legs;
cout << d.tail;
}
Output:
41
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.
In private mode, the protected and public members of super class become private members of
derived class.
3) Protected Inheritance
In protected mode, the public and protected members of Super class becomes protected
members of Sub class.
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance (also known as Virtual Inheritance)
In this type of inheritance one derived class inherits from only one base class. It is the most
simplest form of Inheritance.
Multiple Inheritance in C++
In this type of inheritance a single derived class may inherit from two or more than two base
classes.
In this type of inheritance, multiple derived classes inherits from a single base class.
Multilevel Inheritance in C++
In this type of inheritance a class is derived from a class which is also a derived class of another class.
Although the output of following program may be different on different compilers, when
compiled using Dev-CPP, it prints following:
Constructing base
Constructing derived
Destructing base
Virtual destructor in base class ensures that the destructor of derived class will be called when using
base pointer to the object.For example,
Output:
Constructing base
Constructing derived
Destructing derived
Destructing base
C++ Virtual Base Class
Virtual base class is used in situation where a derived have multiple copies of base class.
Consider the following figure:
From the fig it is clear that class d will have 2 copies of class a-one from b and other from c which
leads to ambiguity.because when we try to call any member of class a using class d obj the compiler
#include <iostream>
using namespace std;
class A {
public:
void show()
{
cout << "Hello form A \n";
}
};
class B : public A {
};
class C : public A {
};
class D : public B, public C {
};
int main()
{
D object;
object.show();
}
Compile Errors:
the above problem can be overcome by making base classes B,C as virtual.
STATIC BINDING:
Static binding occurs at compile time.so this is also called as early binding.
In static binding linking of func call with fun def happens at compile time.
Static bin occurs when all the info needed for the exec of func is available at compile time.
Since all the info is available before runtime itself ,therefore static bin results in faster exec of code
Ex:
output:
*/
#include<iostream>
class Complex
int real,img;
public:
Complex()
real=0;
img=0;
this->real=real;
this->img=img;
void display()
Complex temp;
temp.real=real+c.real;
temp.img=img+c.img;
return temp;
}
};
int main()
Complex c1(2,3);
c1.display();
Complex c2(5,2);
c2.display();
Complex c3;
c3=c1+c2;
c3.display();
cout<<"\n";
return 0;
DYNAMIC BINDING:
Dynamic binding occurs at run time.so this is also called as late binding.
In dynamic binding linking of func call with fun def happens at run time.
Dynamic bin occurs when all the info needed for the exec of func cannot be determined at compile
time.
Since all the info is available at runtime itself ,therefore dynamic bin results in slower exec of code
#include <iostream>
using namespace std;
class Animals
public:
};
public:
void sound()
};
int main()
Animals *a;
Dogs d;
a= &d;
a -> sound();
return 0;
Op:
Dogs bark
Virtual Function is a member function of the base class which is overridden in the derived class. The
compiler performs late binding on this function.
To make a function virtual, we write the keyword virtual before the function definition.
A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t
have implementation, we only declare it. A pure virtual function is declared by assigning 0 in
declaration.
We must implement all pure virtual functions in derived class.otherwise the compiler shows
error.
A class with at least one pure virtual function or abstract function is called abstract class. We
can't create an object of abstract class
// An abstract class
class Test
{
public:
};
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived: public Base
{
public:
void show() { cout << "In Derived \n"; }
};
e
int main(void)
{
Base *bp = new Derived();
bp->show();
return 0;
}
Output:
In Derived
public:
virtual void Display1()=0; //Pure virtual function or
abstract function
virtual void Display2()=0; //Pure virtual function or
abstract function
void Display3()
{
cout<<"\n\tThis is Display3() method of Base Class";
}
};
public:
void Display1()
{
cout<<"\n\tThis is Display1() method of Derived
Class";
}
void Display2()
{
cout<<"\n\tThis is Display2() method of Derived
Class";
}
};
void main()
{
DerivedClass D;
Output :