SlideShare a Scribd company logo
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.

More Related Content

What's hot

Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
Shrija Madhu
 
Inheritance
Inheritance Inheritance
Inheritance
sourav verma
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
Hirra Sultan
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
Nivegeetha
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiers
Srinivas Reddy
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
Ganesh Hogade
 
OCA JAVA - 1 Packages and Class Structure
 OCA JAVA - 1 Packages and Class Structure OCA JAVA - 1 Packages and Class Structure
OCA JAVA - 1 Packages and Class Structure
Fernando Gil
 
Introduction to Java Part-3
Introduction to Java Part-3Introduction to Java Part-3
Introduction to Java Part-3
RatnaJava
 
Packages in java
Packages in javaPackages in java
Packages in java
Jancypriya M
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
KartikKapgate
 
Is2215 lecture4 student (1)
Is2215 lecture4 student (1)Is2215 lecture4 student (1)
Is2215 lecture4 student (1)
dannygriff1
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
BG Java EE Course
 

What's hot (12)

Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
Inheritance
Inheritance Inheritance
Inheritance
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiers
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
OCA JAVA - 1 Packages and Class Structure
 OCA JAVA - 1 Packages and Class Structure OCA JAVA - 1 Packages and Class Structure
OCA JAVA - 1 Packages and Class Structure
 
Introduction to Java Part-3
Introduction to Java Part-3Introduction to Java Part-3
Introduction to Java Part-3
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Is2215 lecture4 student (1)
Is2215 lecture4 student (1)Is2215 lecture4 student (1)
Is2215 lecture4 student (1)
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 

Similar to Inheritance and Polymorphism in Oops

Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
JP2B1197685ARamSaiPM
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
VishnuSupa
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
Tansh5
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
Gajendra Singh Thakur
 
INHERITANCE
INHERITANCEINHERITANCE
INHERITANCE
RohitK71
 
Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03
sivakumarmcs
 
Inheritance
InheritanceInheritance
Inheritance
Munsif Ullah
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismXamarin: Inheritance and Polymorphism
Xamarin: Inheritance and Polymorphism
Eng Teong Cheah
 
Ritik (inheritance.cpp)
Ritik (inheritance.cpp)Ritik (inheritance.cpp)
Ritik (inheritance.cpp)
RitikAhlawat1
 
04 Java Language And OOP Part IV
04 Java Language And OOP Part IV04 Java Language And OOP Part IV
04 Java Language And OOP Part IV
Hari Christian
 
OOP and C++Classes
OOP and C++ClassesOOP and C++Classes
OOP and C++Classes
MuhammadHuzaifa981023
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
ssuser6f3c8a
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
Srinivas Reddy
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
EmanAsem4
 
Chapter 05 polymorphism
Chapter 05 polymorphismChapter 05 polymorphism
Chapter 05 polymorphism
Nurhanna Aziz
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
Redwan Islam
 
Introduction to Inheritance
Introduction to InheritanceIntroduction to Inheritance
Introduction to Inheritance
Keshav Vaswani
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
CMSC 202 - Lec16 - Polymorphisms(1).pptx
CMSC 202 - Lec16 - Polymorphisms(1).pptxCMSC 202 - Lec16 - Polymorphisms(1).pptx
CMSC 202 - Lec16 - Polymorphisms(1).pptx
deathlyfire321
 
Access Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and EncapsulationAccess Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and Encapsulation
Abid Kohistani
 

Similar to Inheritance and Polymorphism in Oops (20)

Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
INHERITANCE
INHERITANCEINHERITANCE
INHERITANCE
 
Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03
 
Inheritance
InheritanceInheritance
Inheritance
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismXamarin: Inheritance and Polymorphism
Xamarin: Inheritance and Polymorphism
 
Ritik (inheritance.cpp)
Ritik (inheritance.cpp)Ritik (inheritance.cpp)
Ritik (inheritance.cpp)
 
04 Java Language And OOP Part IV
04 Java Language And OOP Part IV04 Java Language And OOP Part IV
04 Java Language And OOP Part IV
 
OOP and C++Classes
OOP and C++ClassesOOP and C++Classes
OOP and C++Classes
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Chapter 05 polymorphism
Chapter 05 polymorphismChapter 05 polymorphism
Chapter 05 polymorphism
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
Introduction to Inheritance
Introduction to InheritanceIntroduction to Inheritance
Introduction to Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
CMSC 202 - Lec16 - Polymorphisms(1).pptx
CMSC 202 - Lec16 - Polymorphisms(1).pptxCMSC 202 - Lec16 - Polymorphisms(1).pptx
CMSC 202 - Lec16 - Polymorphisms(1).pptx
 
Access Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and EncapsulationAccess Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and Encapsulation
 

Recently uploaded

The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
Madan Karki
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
Hematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood CountHematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood Count
shahdabdulbaset
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
amsjournal
 
Introduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptxIntroduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptx
MiscAnnoy1
 

Recently uploaded (20)

The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
Hematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood CountHematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood Count
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
 
Introduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptxIntroduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptx
 

Inheritance and Polymorphism in Oops

  • 2.
  • 3. 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
  • 5. • 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
  • 6. Creating all three Classes of Vehicle by avoiding INHERITANCE INHERITANCE We have to write all of these functions in each of the three Classes
  • 7. • 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
  • 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 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.
  • 10. 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 };
  • 13. 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
  • 14. 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
  • 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 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
  • 20. 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
  • 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.