INHERITANCE
IN
C++
LALFAKAWMA
M.Tech (C.S.E)
INHERITANCE
• Super Class:
The class whose properties are inherited by sub class is called
Base Class or Super class.
• Sub Class:
The class that inherits properties from another class is called Sub
class or Derived Class.
• The capability of a class to derive properties and characteristics from another
class is called Inheritance.
• Also known as Reusability. Reuse code that already exists rather than trying to
create the same all over again
Why
And
When
TO USE
INHERITANCE
?
• Consider a Group of Vehicles. We need to Create Classes
for Bus, Car and Truck.
• The methods FuelAmount(), Capacity(), ApplyBrakes()
will be same for all of the three Classes.
• If we create all these Classes avoiding INHERITANCE
then, we have to write all of these functions in each of
the three Classes
INHERITANCE
Creating all three Classes of Vehicle
by avoiding INHERITANCE
INHERITANCE
We have to write all of these functions in each of the three Classes
• Duplication of same code 3 times. This increases the
chances of error and data redundancy.
WITHOUT INHERITANCE
• To avoid this type of situation, inheritance is used.
• If we create a class Vehicle and write these three functions in it
and inherit the rest of the classes from the vehicle class, then we
can simply avoid the duplication of data and increase re-usability
INHERITANCE
• Using inheritance, we can write the functions only one time instead of three
times as we have inherited rest of the three classes from base class(Vehicle).
• Example:
class Bus :public Vehicle
{
//body of Derived Class
};
Base Class
Derived Class
INHERITANCE
MODE OF INHERITANCE
Public Mode
Private Mode
Protected 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.
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.
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.
Note : The private members in the base class cannot be directly accessed in the derived class, while
protected members can be directly accessed.
INHERITANCEclass A
{
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A
{
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A
{
// x is protected
// y is protected
// z is not accessible from C
};
class D : private A // 'private' is default for classes
{
// x is private
// y is private
// z is not accessible from D
};
INHERITANCE
TYPES OF INHERITANCE
TYPE OF INHERITANCE
1.SINGLE INHERITANCE
• Single Inheritance:
In single inheritance, a class is allowed to inherit from only one class. i.e. one sub
class is inherited by one base class only.
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class Car: public Vehicle
{
};
int main()
{
Car C;
return 0;
}
Vehicle Class
Car Class
Output:
This is a Vehicle
2.MULTIPLE INHERITANCE
• Multiple Inheritance:
Multiple Inheritance is a feature of C++ where a class can inherit from more than one
classes. i.e one Derived class is inherited from more than one Base classes.
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class FourWheeler
{
cout<<“This is a 4 Wheeler Vehicle”<<endl;
};
int main()
{
Car C;
return 0;
}
Output:
This is a Vehicle
This is a 4 wheeler Vehicle
class Car: public Vehicle,public FourWheeler
{
};
Vehicle Class
Car Class
FourWheeler
3. MULTILEVEL INHERITANCE
• Multilevel Inheritance:
In this type of inheritance, a Derived Class is created from another Derived Class.
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class FourWheeler : public Vehicle
{
cout<<“4 wheels are Vehicle”<<endl;
};
int main()
{
Car C;
return 0;
}
Output:
This is a Vehicle
4 wheels are Vehicle
Car has 4 Wheels
class Car: public FourWheeler
{
public:
Car()
{
cout<<“Car has 4 Wheels”<<endl;
}
};
Vehicle Class
Car Class
FourWheeler
4. HIERARCHICAL INHERITANCE
• Hierarchical Inheritance: In this type of inheritance, more than one sub class is inherited from a
single base class. i.e. more than one derived class is created from a single base class.
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class Bus : public Vehicle
{
};
int main()
{
Car C;
Bus B;
return 0;
}
Output:
This is a Vehicle
This is a Vehicle
class Car: public Vehicle
{
};
Vehicle Class
Car ClassBus Class
5. HYBRID INHERITANCE
• Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more than one type
of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class Fare
{
public:
Fare()
{
cout<<“Fare of Vehiclen”;
}
};
int main()
{
Bus B;
return 0;
}
Output:
This is a Vehicle
Fare of Vehicle
class Car: public Vehicle
{
};
Vehicle Class
Car Class
Fare Class
class Bus : public Vehicle , public Fare
{
};
Bus Class
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.
• Real life example of polymorphism:
• A person at the same time can have different characteristic.
• Like a man at the same time:
 A Father,.
 A Husband,
 An Employee.
• So the same person posses different behaviour in different situations.
This is called polymorphism
POLYMORPHISM
POLYMORPHISM
• Compile time polymorphism:
• This type of polymorphism is achieved by Function Overloading or Operator
Overloading
• Function Overloading:
• When there are multiple functions with same name but different
parameters then these functions are said to be overloaded.
• Functions can be Overloaded by:
 Change in number of arguments or/and
 Change in type of arguments
• Operator Overloading:
• Operator (‘+’) for string class to Concatenate two strings.
• Operator (‘+’) for Integer operand to Add the two integer.
(‘+’) Operator
IntegerString
2 + 3 =5a + b =ab
• Runtime polymorphism: This type of polymorphism is achieved by Function
Overriding.
POLYMORPHISM
• Function overriding on the other hand 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.

Inheritance and Polymorphism in Oops

  • 1.
  • 3.
    INHERITANCE • Super Class: Theclass whose properties are inherited by sub class is called Base Class or Super class. • Sub Class: The class that inherits properties from another class is called Sub class or Derived Class. • The capability of a class to derive properties and characteristics from another class is called Inheritance. • Also known as Reusability. Reuse code that already exists rather than trying to create the same all over again
  • 4.
  • 5.
    • Consider aGroup of Vehicles. We need to Create Classes for Bus, Car and Truck. • The methods FuelAmount(), Capacity(), ApplyBrakes() will be same for all of the three Classes. • If we create all these Classes avoiding INHERITANCE then, we have to write all of these functions in each of the three Classes INHERITANCE
  • 6.
    Creating all threeClasses of Vehicle by avoiding INHERITANCE INHERITANCE We have to write all of these functions in each of the three Classes
  • 7.
    • Duplication ofsame code 3 times. This increases the chances of error and data redundancy. WITHOUT INHERITANCE • To avoid this type of situation, inheritance is used. • If we create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class, then we can simply avoid the duplication of data and increase re-usability
  • 8.
    INHERITANCE • Using inheritance,we can write the functions only one time instead of three times as we have inherited rest of the three classes from base class(Vehicle). • Example: class Bus :public Vehicle { //body of Derived Class }; Base Class Derived Class
  • 9.
    INHERITANCE MODE OF INHERITANCE PublicMode Private Mode Protected 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. 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. 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. Note : The private members in the base class cannot be directly accessed in the derived class, while protected members can be directly accessed.
  • 10.
    INHERITANCEclass A { public: int x; protected: inty; private: int z; }; class B : public A { // x is public // y is protected // z is not accessible from B }; class C : protected A { // x is protected // y is protected // z is not accessible from C }; class D : private A // 'private' is default for classes { // x is private // y is private // z is not accessible from D };
  • 11.
  • 12.
  • 13.
    1.SINGLE INHERITANCE • SingleInheritance: In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is inherited by one base class only. #include <iostream> using namespace std; class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class Car: public Vehicle { }; int main() { Car C; return 0; } Vehicle Class Car Class Output: This is a Vehicle
  • 14.
    2.MULTIPLE INHERITANCE • MultipleInheritance: Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one Derived class is inherited from more than one Base classes. #include <iostream> using namespace std; class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class FourWheeler { cout<<“This is a 4 Wheeler Vehicle”<<endl; }; int main() { Car C; return 0; } Output: This is a Vehicle This is a 4 wheeler Vehicle class Car: public Vehicle,public FourWheeler { }; Vehicle Class Car Class FourWheeler
  • 15.
    3. MULTILEVEL INHERITANCE •Multilevel Inheritance: In this type of inheritance, a Derived Class is created from another Derived Class. #include <iostream> using namespace std; class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class FourWheeler : public Vehicle { cout<<“4 wheels are Vehicle”<<endl; }; int main() { Car C; return 0; } Output: This is a Vehicle 4 wheels are Vehicle Car has 4 Wheels class Car: public FourWheeler { public: Car() { cout<<“Car has 4 Wheels”<<endl; } }; Vehicle Class Car Class FourWheeler
  • 16.
    4. HIERARCHICAL INHERITANCE •Hierarchical Inheritance: In this type of inheritance, more than one sub class is inherited from a single base class. i.e. more than one derived class is created from a single base class. #include <iostream> using namespace std; class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class Bus : public Vehicle { }; int main() { Car C; Bus B; return 0; } Output: This is a Vehicle This is a Vehicle class Car: public Vehicle { }; Vehicle Class Car ClassBus Class
  • 17.
    5. HYBRID INHERITANCE •Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance. class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class Fare { public: Fare() { cout<<“Fare of Vehiclen”; } }; int main() { Bus B; return 0; } Output: This is a Vehicle Fare of Vehicle class Car: public Vehicle { }; Vehicle Class Car Class Fare Class class Bus : public Vehicle , public Fare { }; Bus Class
  • 18.
    POLYMORPHISM • The wordpolymorphism 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. • Real life example of polymorphism: • A person at the same time can have different characteristic. • Like a man at the same time:  A Father,.  A Husband,  An Employee. • So the same person posses different behaviour in different situations. This is called polymorphism
  • 19.
  • 20.
    POLYMORPHISM • Compile timepolymorphism: • This type of polymorphism is achieved by Function Overloading or Operator Overloading • Function Overloading: • When there are multiple functions with same name but different parameters then these functions are said to be overloaded. • Functions can be Overloaded by:  Change in number of arguments or/and  Change in type of arguments • Operator Overloading: • Operator (‘+’) for string class to Concatenate two strings. • Operator (‘+’) for Integer operand to Add the two integer. (‘+’) Operator IntegerString 2 + 3 =5a + b =ab
  • 21.
    • Runtime polymorphism:This type of polymorphism is achieved by Function Overriding. POLYMORPHISM • Function overriding on the other hand 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.