SlideShare a Scribd company logo
1 of 15
Object-Oriented
Programming: Inheritance
Outline
• Introduction
• Protected members in inheritance
• Relationship between Base Classes and Derived Classes
• Benefits and Drawbacks of Inheritance
• Constructors and Destructors in Derived Classes
• Public, Protected and Private Inheritance
• Software Engineering with Inheritance
Introduction
• Inheritance is a core concept in object-oriented programming (OOP).
It allows a class to inherit properties and behaviors from another
class. This is a powerful way to reuse code and promote code
abstraction.
• Inheritance works by creating a subclass that inherits from a
superclass. The subclass inherits all of the attributes and methods of
the superclass, and it can also add its own attributes and methods.
• Parent – Child Relationship.
Types of inheritance
Here are some of the types of inheritance:
• Single inheritance: A subclass inherits from a single superclass.
• Multiple inheritance: A subclass inherits from multiple superclasses.
• Hierarchical inheritance: A class hierarchy is formed when a subclass
inherits from another subclass.
• Hybrid inheritance: A mix of two or more of the above types of
inheritance occurs.
Syntax
class Subclass’Name : access_specifier BaseClass’Name {
// members of subclass
};
The access_specifier can be one of the following:
• public: The subclass inherits all of the public members of the base
class.
• protected: The subclass inherits all of the protected members of the
base class.
• private: The subclass inherits none of the members of the base class.
Example
Animal class is Base Class/parent
class and Dog class is Derived
class/sub class.
Protected members in inheritance,oop,c++
In C++, protected members are members of a class that can only be
accessed by the class itself, its subclasses, and friends.
• Protected members are similar to private members, but they can be
inherited by subclasses. Private members are not accessible to
subclasses or friends.
• The syntax for declaring a protected member in C++ is:
class MyClass {
protected:
int x;
};
Relationship between Base Classes and
Derived Classes
In C++, a base class is a class from which other classes are derived. The
derived classes are called subclasses. The base class is the "parent" of the
subclasses, and the subclasses are the "children" of the base class.
The relationship between a base class and its subclasses is one of
inheritance. This means that the subclasses inherit all of the properties and
behaviors of the base class. The subclasses can then add their own properties and
behaviors, or they can override the properties and behaviors of the base class.
Benefits and Drawbacks of Inheritance
Here are some of the benefits of using inheritance in C++:
I. It allows us to reuse code.
II. It helps us to organize our code.
III. It can help us to make our code more robust and easier to
maintain.
Here are some of the drawbacks of using inheritance in C++:
I. It can make the code more complex.
II. It can make it difficult to track down the source of errors.
Constructors and Destructors in Derived Classes
When a subclass inherits from a base class, the subclass inherits the base class's constructors and
destructors. However, the subclass can also define its own constructors and destructors.
If the subclass defines its own constructors, the base class's constructors will be called
automatically when the subclass object is created.
The order in which the constructors are called is as follows:
• The base class's default constructor is called.
• The subclass's default constructor is called.
• If the subclass defines any parameterized constructors, they are called in the order that they are
defined.
If the subclass defines its own destructor, the base class's destructor will be called automatically
when the subclass object is destroyed. The order in which the destructors are called is as follows:
• The subclass's destructor is called.
• The base class's destructor is called.
Example
When a DerivedClass object is created,
the following happens:
• The BaseClass class's default constructor
is called.
• The DerivedClass class's default
constructor is
called.
When a DerivedClass object is destroyed,
the following happens:
• The DerivedClass class's destructor is
called.
• The BaseClass class's destructor is
called.
Public, Protected and Private Inheritance
• Public inheritance is the default type of inheritance in C++. When a class
inherits from another class in public mode, the subclass inherits all of the
public members of the base class. This means that the subclass can access
the public members of the base class as if they were its own members.
• Protected inheritance is a type of inheritance where the subclass inherits
all of the protected members of the base class. This means that the
subclass can access the protected members of the base class, but it cannot
access the public members of the base class.
• Private inheritance is a type of inheritance where the subclass inherits all
of the private members of the base class. This means that the subclass can
only access the private members of the base class through the base class's
methods.
Public, Protected and Private Inheritance…..
Type of inheritance Access to base class members
Public inheritance Public and protected members
Protected inheritance Protected members
Private inheritance Private members
Software Engineering with Inheritance
• Inheritance is a powerful tool in software engineering that can be used to
improve the design and implementation of our code. It allows us to reuse code
that has already been written, and it can also help us to abstract away the details
of how something works.
Here are some of the ways that inheritance can be used in software engineering:
• To create a hierarchy of classes: Inheritance can be used to create a hierarchy of
classes, where each class inherits from a more general class. This can help to
organize our code and make it easier to understand.
• To implement polymorphism: Inheritance can be used to implement
polymorphism, which allows us to treat different objects in the same way. This
can make our code more flexible and easier to use.
• To reuse code: Inheritance can be used to reuse code that has already been
written. This can save us time and effort, and it can also help to keep our code
consistent.
End

More Related Content

Similar to Object oriented programming new syllabus presentation

Similar to Object oriented programming new syllabus presentation (20)

Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP concepts
 
INHERITANCES.pptx
INHERITANCES.pptxINHERITANCES.pptx
INHERITANCES.pptx
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Oopsinphp
OopsinphpOopsinphp
Oopsinphp
 
java part 1 computer science.pptx
java part 1 computer science.pptxjava part 1 computer science.pptx
java part 1 computer science.pptx
 
Inheritance
Inheritance Inheritance
Inheritance
 
C# structure
C# structureC# structure
C# structure
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Topic inheritance
Topic  inheritanceTopic  inheritance
Topic inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
27c
27c27c
27c
 
27csharp
27csharp27csharp
27csharp
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#
 
Object-oriented programming 3.pptx
Object-oriented programming 3.pptxObject-oriented programming 3.pptx
Object-oriented programming 3.pptx
 
B.sc CSIT 2nd semester C++ Unit5
B.sc CSIT  2nd semester C++ Unit5B.sc CSIT  2nd semester C++ Unit5
B.sc CSIT 2nd semester C++ Unit5
 
Oops
OopsOops
Oops
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - Encapsulation
 
Inheritance,constructor,friend function
Inheritance,constructor,friend functionInheritance,constructor,friend function
Inheritance,constructor,friend function
 

Recently uploaded

What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 

Recently uploaded (20)

What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 

Object oriented programming new syllabus presentation

  • 2. Outline • Introduction • Protected members in inheritance • Relationship between Base Classes and Derived Classes • Benefits and Drawbacks of Inheritance • Constructors and Destructors in Derived Classes • Public, Protected and Private Inheritance • Software Engineering with Inheritance
  • 3. Introduction • Inheritance is a core concept in object-oriented programming (OOP). It allows a class to inherit properties and behaviors from another class. This is a powerful way to reuse code and promote code abstraction. • Inheritance works by creating a subclass that inherits from a superclass. The subclass inherits all of the attributes and methods of the superclass, and it can also add its own attributes and methods. • Parent – Child Relationship.
  • 4. Types of inheritance Here are some of the types of inheritance: • Single inheritance: A subclass inherits from a single superclass. • Multiple inheritance: A subclass inherits from multiple superclasses. • Hierarchical inheritance: A class hierarchy is formed when a subclass inherits from another subclass. • Hybrid inheritance: A mix of two or more of the above types of inheritance occurs.
  • 5. Syntax class Subclass’Name : access_specifier BaseClass’Name { // members of subclass }; The access_specifier can be one of the following: • public: The subclass inherits all of the public members of the base class. • protected: The subclass inherits all of the protected members of the base class. • private: The subclass inherits none of the members of the base class.
  • 6. Example Animal class is Base Class/parent class and Dog class is Derived class/sub class.
  • 7. Protected members in inheritance,oop,c++ In C++, protected members are members of a class that can only be accessed by the class itself, its subclasses, and friends. • Protected members are similar to private members, but they can be inherited by subclasses. Private members are not accessible to subclasses or friends. • The syntax for declaring a protected member in C++ is: class MyClass { protected: int x; };
  • 8. Relationship between Base Classes and Derived Classes In C++, a base class is a class from which other classes are derived. The derived classes are called subclasses. The base class is the "parent" of the subclasses, and the subclasses are the "children" of the base class. The relationship between a base class and its subclasses is one of inheritance. This means that the subclasses inherit all of the properties and behaviors of the base class. The subclasses can then add their own properties and behaviors, or they can override the properties and behaviors of the base class.
  • 9. Benefits and Drawbacks of Inheritance Here are some of the benefits of using inheritance in C++: I. It allows us to reuse code. II. It helps us to organize our code. III. It can help us to make our code more robust and easier to maintain. Here are some of the drawbacks of using inheritance in C++: I. It can make the code more complex. II. It can make it difficult to track down the source of errors.
  • 10. Constructors and Destructors in Derived Classes When a subclass inherits from a base class, the subclass inherits the base class's constructors and destructors. However, the subclass can also define its own constructors and destructors. If the subclass defines its own constructors, the base class's constructors will be called automatically when the subclass object is created. The order in which the constructors are called is as follows: • The base class's default constructor is called. • The subclass's default constructor is called. • If the subclass defines any parameterized constructors, they are called in the order that they are defined. If the subclass defines its own destructor, the base class's destructor will be called automatically when the subclass object is destroyed. The order in which the destructors are called is as follows: • The subclass's destructor is called. • The base class's destructor is called.
  • 11. Example When a DerivedClass object is created, the following happens: • The BaseClass class's default constructor is called. • The DerivedClass class's default constructor is called. When a DerivedClass object is destroyed, the following happens: • The DerivedClass class's destructor is called. • The BaseClass class's destructor is called.
  • 12. Public, Protected and Private Inheritance • Public inheritance is the default type of inheritance in C++. When a class inherits from another class in public mode, the subclass inherits all of the public members of the base class. This means that the subclass can access the public members of the base class as if they were its own members. • Protected inheritance is a type of inheritance where the subclass inherits all of the protected members of the base class. This means that the subclass can access the protected members of the base class, but it cannot access the public members of the base class. • Private inheritance is a type of inheritance where the subclass inherits all of the private members of the base class. This means that the subclass can only access the private members of the base class through the base class's methods.
  • 13. Public, Protected and Private Inheritance….. Type of inheritance Access to base class members Public inheritance Public and protected members Protected inheritance Protected members Private inheritance Private members
  • 14. Software Engineering with Inheritance • Inheritance is a powerful tool in software engineering that can be used to improve the design and implementation of our code. It allows us to reuse code that has already been written, and it can also help us to abstract away the details of how something works. Here are some of the ways that inheritance can be used in software engineering: • To create a hierarchy of classes: Inheritance can be used to create a hierarchy of classes, where each class inherits from a more general class. This can help to organize our code and make it easier to understand. • To implement polymorphism: Inheritance can be used to implement polymorphism, which allows us to treat different objects in the same way. This can make our code more flexible and easier to use. • To reuse code: Inheritance can be used to reuse code that has already been written. This can save us time and effort, and it can also help to keep our code consistent.
  • 15. End