Module - 3 C++
Module - 3 C++
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.
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.
Why and when to use inheritance?
Consider a group of vehicles. You need to create classes for Bus, Car, and Truck. The methods
fuelAmount(), capacity(), applyBrakes() will be the same for all three classes. If we create
these classes avoiding inheritance then we have to write all of these functions in each of the
three classes as shown below figure:
Syntax:
class <derived_class_name> : <access-specifier> <base_class_name>
{
//body
}
Example:
1. class ABC : private XYZ //private derivation
{ }
2. class ABC : public XYZ //public derivation
{ }
3. class ABC : protected XYZ //protected derivation
{ }
4. class ABC: XYZ //private derivation by default
{ }
// Example: define member function without argument within
// the class
#include <iostream>
using namespace std;
class Person {
int id;
char name[100];
public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}
void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name <<endl;
}
};
public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}
void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee << endl;
}
};
int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}
Output: Enter the Id: 101
Enter the Name: Dev
Enter the Course Name: GCS
Enter the Course Fee:70000
Id: 101
Name: Dev
Course: GCS
Fee: 70000
Modes of Inheritance: There are 3 modes of inheritance.
1. 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.
2. Protected Mode: If we derive a subclass from a Protected base class.
Then both public members and protected members of the base class will
become protected in the derived class.
3. Private Mode: If we derive a subclass from a Private base class. Then
both public members and protected members of the base class will
become Private in the derived class.
Types Of Inheritance:-
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
Types of Inheritance in C++
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.
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output
This is a Vehicle
Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class
can inherit from more than one class. i.e one subclass is inherited from
more than one base class.
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
This is a 4 wheeler Vehicle
Multilevel Inheritance: In this type of inheritance, a derived class is created
from another derived class.
Syntax:-
class C
{
... .. ...
};
class B:public C
{
... .. ...
};
class A: public B
{
... ... ...
};
// C++ program to implement
// Multilevel Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
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.
}
// C++ program to implement
// Hierarchical Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// first sub class
class Car : public Vehicle {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
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
inheritances:
// C++ program for Hybrid Inheritance
#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"; }
};
C++ 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.
Types of Polymorphism
Compile-time Polymorphism
Runtime Polymorphism
1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator
overloading.
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.
// C++ program to demonstrate
// function overloading or
// Compile-time Polymorphism
#include <bits/stdc++.h>
// Driver code
int main()
{
Geeks obj1;
class Test {
public:
Test() { cout << "\n Constructor executed"; }
return 0;
}
Constructor executed
Destructor executed