Inheritance Slides
Inheritance Slides
Muhammad Farooq
MAIN TOPICS
What is Inheritance?
Advantages
Types of Inheritance/Access modifiers
Categories of Inheritance
Function over-riding
INHERITANCE
Inheritance is the Key feature of OOP
The process of creating new classes from the existing classes is called
inheritance
Existing classes: are called
⚫ Parent or
⚫ Base or
⚫ Super classes
Need: Reusability
The derived class inherits some of the features from the base class
and can have additional features of its own.
private
Base Class
protected
public
Derived Class
protected
public
private
public
private
protected Derived class members
Derived public
Class
protected Derived from Base
class
public
•Student
•Rollo
•Name
•subjects Base Class
•Student
•Discipline
•Rollo Derived
•Name
•subjects Class
•Animal
•Four legs Base Class
•Hair on body
Derived
•Vertebrates
•Have Backbone Class
SYNTAX
Example
Class A : public class B
Output
Main function
Derived function
5
EXAMPLE
class Animal
{
public:
int legs = 4;
};
int main()
{
Dog d;
cout << d.legs;
cout << d.tail;
ACCESS MODIFIERS
VISIBILITY OF CLASS MEMBERS
It can either be
⚫ private
⚫ protected
⚫ 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.
Used by
Protecte functions
d Private
+
Public
Protected mode
Derived class
Base class
Used by
functions
Protecte
Protecte
d
d
+
Public
Public mode
Used by
Protecte Protecte functions
d d
Public
Used by
Derived class
Base class object
Public Public
SUMMARY
Private members of base class can not be used by
derived class
Protected members of base class can be used by
derived class
Public members of base class can be used by
derived class using object
C++ FUNCTION OVERRIDING
int main()
{
B obj;
obj.A::show();
obj.show();
}
DIFFERENCE BETWEEN
FUNCTION OVERLOADING & FUNCTION OVERRIDING
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid (Virtual) Inheritance
SINGLE INHERITANCE