SlideShare a Scribd company logo
1 of 21
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 inheritanceShrija Madhu
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiersSrinivas Reddy
 
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 StructureFernando Gil
 
Introduction to Java Part-3
Introduction to Java Part-3Introduction to Java Part-3
Introduction to Java Part-3RatnaJava
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and PolymorphismKartikKapgate
 
Is2215 lecture4 student (1)
Is2215 lecture4 student (1)Is2215 lecture4 student (1)
Is2215 lecture4 student (1)dannygriff1
 

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 in c++
Inheritance in c++Inheritance in c++
Inheritance in c++VishnuSupa
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptxTansh5
 
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
INHERITANCERohitK71
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismXamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismEng 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 IVHari Christian
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerssuser6f3c8a
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.pptEmanAsem4
 
Chapter 05 polymorphism
Chapter 05 polymorphismChapter 05 polymorphism
Chapter 05 polymorphismNurhanna 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 InheritanceKeshav Vaswani
 
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 EncapsulationAbid Kohistani
 
(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_ii(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_iiNico Ludwig
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 

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
 
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
 
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
 
(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_ii(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_ii
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 

Recently uploaded

Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 

Recently uploaded (20)

Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 

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.