Inheritance
Inheritance
1 Inheritance
• The capability of a class to derive properties and characteristics from another class
is called Inheritance.
• Inheritance is one of the most important features of Object-Oriented Programming.
• Inheritance is a feature or a process in which, new classes are created from the
existing classes.
• The new class created is called “derived class” or “child class” and the existing class
is known as the “base class” or “parent class”.
• The derived class now is said to be inherited from the base class.
• It means, the derived class inherits all the properties of the base class, without
changing the properties of base class and may add new features to its own.
• These new features in the derived class will not affect the base class. The derived
class is the specialized class for the base class.
• Sub Class: The class that inherits properties from another class is called
Subclass or Derived Class.
• Super Class: The class whose properties are inherited by a subclass is called
Base Class or Superclass.
• Implementing inheritance in C++: For creating a sub-class that is inherited from
the base class we have to follow the below syntax.
• Derived Classes: A Derived class is defined as the class derived from the base class.
Syntax:
class <derived_class_name> : <access-specifier> <base_class_name>
{
//body
}
Where
class — keyword to create a new class
derived_class_name — name of the new class, which will inherit the base class
access-specifier — either of private, public or protected. If neither is specified,
PRIVATE is taken as default
base-class-name — name of the base class
• Note: A derived class doesn’t inherit access to private data members. However, it
does inherit a full parent object, which contains any private members which that
class declares.
Types
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid 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.
Syntax:
OR
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
Syntax:
class C
{
... .. ...
};
Syntax: -
class C
{
... .. ...
};
class B:public C
{
... .. ...
};
class A: public B
{
... ... ...
};
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.
Syntax:-
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj2;
return 0;
}
1.2 Polymorphism
The word “polymorphism” means having many forms.
In simple words, we can define polymorphism as the ability of a message to be
displayed in more than one form.
A real-life example of polymorphism is a person who at the same time can have
different characteristics.
A man at the same time is a father, a husband, and an employee. So the same
person exhibits different behavior in different situations. This is called
polymorphism.
Polymorphism is considered one of the important features of Object-Oriented
Programming.
Types of Polymorphism
1. Compile-time Polymorphism
2. Runtime Polymorphism
1. Compile-time Polymorphism
a) Function Overloading
When there are multiple functions with the same name but different
parameters, then the functions are said to be overloaded, hence this is
known as Function Overloading.
Functions can be overloaded by changing the number of
arguments or/and changing the type of arguments.
In simple terms, it is a feature of object-oriented programming providing
many functions that have the same name but distinct parameters when
numerous tasks are listed under one function name.
There are certain Rules of Function Overloading that should be followed
while overloading a function.
b) Operator Overloading
C++ has the ability to provide the operators with a special meaning for
a data type, this ability is known as operator overloading.
For example, we can make use of the addition operator (+) for string
class to concatenate two strings.
We know that the task of this operator is to add two operands.
So a single operator ‘+’, when placed between integer operands, adds
them and when placed between string operands, concatenates them.
2. Runtime Polymorphism
This type of polymorphism is achieved by Function Overriding.
Late binding and dynamic polymorphism are other names for runtime
polymorphism.
The function call is resolved at runtime in runtime polymorphism.
In contrast, with compile time polymorphism, the compiler determines which
function call to bind to the object after deducing it at runtime.
a) Function Overriding
Function Overriding occurs when a derived class has a definition for one
of the member functions of the base class. That base function is said to
be overridden.
b) Virtual Function
A virtual function is a member function that is declared in the base class
using the keyword virtual and is re-defined (Overridden) in the derived
class.