SlideShare a Scribd company logo
Inheritance
Adapted from Textbook (SAVITCH)
Inheritance
โ€ข We may need a process by which a new class
can be created from another class.
โ€ข Inheritance gives an ability to define new classes
using existing classes as a basis; those are
called โ€œderivedโ€ and โ€œbaseโ€ classes, respectively
(a.k.a. parent/ancestor-child/descendant
classes).
โ€ข Inheritance provides code reuse
โ€“ Allows one class to "derive" from another,
adding features
Inheritance
โ€ข Derived class inherits data members and
member functions from base class(es).
โ€ข Derived class objects inherit members of
base class
โ€“And may add members
Inheritance
โ€ข Private member variables in base class cannot
be accessed "by name" in derived
โ€ข Protected members in base class:
โ€“ Can be accessed "by name" in derived class
member functions
โ€ข Constructors are not inherited
โ€“ Are invoked from derived classโ€™ constructor
Single and Multiple
โ€ข When a class is derived from a base class
this inheritance is called โ€œsingle
inheritanceโ€.
โ€ข A class may inherit from more than one
class; this type of inheritance is โ€œmultiple
inheritanceโ€.
Access Restrictions
โ€ข Inheritance does not occur without
limitations.
โ€ข Based on access restrictions there are
three forms:
โ€“ Public
โ€“ Protected
โ€“ Private
Syntax
class X{
โ€ฆ
};
class Y : public X{
โ€ฆ
};
Class Y is derived from class X with โ€œpublicโ€
derivation
Protected Members
โ€ข Members that can be accessed within the
same class and by classes derived from
that class, also by friends of the class.
#include <iostream>
using namespace std;
class A{
protected:
int number;
};
class B:public A{
public:
void f(){number=0; cout << ++number;}
};
main(){
B myb;
myb.f();
}
Example
class A{
public:
int a,b;
};
class B : public A{
public:
int b,c;
};
main(){
B b; // b as object name of type B
b.a=1; // a as inherited from the base class A
b.b=3; // 2nd b is of derived class B
b.A::b=2; // 2nd b is of base class A
b.c=4; // c is of derived class B
}
Multiple Inheritance
โ€ข You may need a class to inherit more than one
base class.
โ€ข C++ provides such flexibility with multiple
inheritance
โ€ข For example, a police car and an ambulance are
both emergency vehicles.
โ€“ Both classes may inherit from vehicle and
emergency parent classes although vehicle and
emergency classes are not linked to each other
Example
car
emergency truck
vehicle
police car ambulance fire engine
class vehicle
{...};
class emergency
{...};
class car:public vehicle
{...};
class truck:public vehicle
{...};
class policeCar:public car, public emergency
{...};
class ambulance:public car, public emergency
{...};
class fireEngine:public truck, public emergency
{...};

More Related Content

PPT
Inheritance in C++
PPTX
Object oriented programming new syllabus presentation
PPT
inheritance
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
PPTX
Inheritance
PPT
Inheritance, Object Oriented Programming
PPTX
EASY TO LEARN INHERITANCE IN C++
PPT
Inheritance
Inheritance in C++
Object oriented programming new syllabus presentation
inheritance
MODULE2_INHERITANCE_SESSION1.ppt computer
Inheritance
Inheritance, Object Oriented Programming
EASY TO LEARN INHERITANCE IN C++
Inheritance

Similar to week14 (1).ppt (20)

PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
PPTX
Lecture 5.mte 407
PPTX
inheritance_OOPC_datastream.ppttttttttttttttx
PPTX
INHERITANCE.pptx
PPTX
Inheritance
PPT
session 24_Inheritance.ppt
PDF
lecture-2021inheritance-160705095417.pdf
PPTX
[OOP - Lec 20,21] Inheritance
PPTX
Mca 2nd sem u-3 inheritance
PDF
lecture 6.pdf
PPTX
Introduction to Inheritance
PPT
Inheritance
PPT
Lab3
PPSX
Inheritance and Polymorphism in Oops
PPTX
Inheritance in c++
PDF
Inheritance
PPTX
TYPES OF INHERITANCE CONCEPT IN C++.pptx
PPTX
Bca 2nd sem u-3 inheritance
PPT
Lecturespecial
PPT
OOP
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
Lecture 5.mte 407
inheritance_OOPC_datastream.ppttttttttttttttx
INHERITANCE.pptx
Inheritance
session 24_Inheritance.ppt
lecture-2021inheritance-160705095417.pdf
[OOP - Lec 20,21] Inheritance
Mca 2nd sem u-3 inheritance
lecture 6.pdf
Introduction to Inheritance
Inheritance
Lab3
Inheritance and Polymorphism in Oops
Inheritance in c++
Inheritance
TYPES OF INHERITANCE CONCEPT IN C++.pptx
Bca 2nd sem u-3 inheritance
Lecturespecial
OOP

Recently uploaded (20)

PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
ย 
PPT
Mechanical Engineering MATERIALS Selection
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
ย 
DOCX
573137875-Attendance-Management-System-original
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Sustainable Sites - Green Building Construction
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Fundamentals of Mechanical Engineering.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT
Total quality management ppt for engineering students
PPTX
Current and future trends in Computer Vision.pptx
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
ย 
Mechanical Engineering MATERIALS Selection
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
ย 
573137875-Attendance-Management-System-original
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
UNIT 4 Total Quality Management .pptx
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Fundamentals of safety and accident prevention -final (1).pptx
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
R24 SURVEYING LAB MANUAL for civil enggi
Sustainable Sites - Green Building Construction
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Fundamentals of Mechanical Engineering.pptx
Safety Seminar civil to be ensured for safe working.
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Total quality management ppt for engineering students
Current and future trends in Computer Vision.pptx

week14 (1).ppt

  • 2. Inheritance โ€ข We may need a process by which a new class can be created from another class. โ€ข Inheritance gives an ability to define new classes using existing classes as a basis; those are called โ€œderivedโ€ and โ€œbaseโ€ classes, respectively (a.k.a. parent/ancestor-child/descendant classes). โ€ข Inheritance provides code reuse โ€“ Allows one class to "derive" from another, adding features
  • 3. Inheritance โ€ข Derived class inherits data members and member functions from base class(es). โ€ข Derived class objects inherit members of base class โ€“And may add members
  • 4. Inheritance โ€ข Private member variables in base class cannot be accessed "by name" in derived โ€ข Protected members in base class: โ€“ Can be accessed "by name" in derived class member functions โ€ข Constructors are not inherited โ€“ Are invoked from derived classโ€™ constructor
  • 5. Single and Multiple โ€ข When a class is derived from a base class this inheritance is called โ€œsingle inheritanceโ€. โ€ข A class may inherit from more than one class; this type of inheritance is โ€œmultiple inheritanceโ€.
  • 6. Access Restrictions โ€ข Inheritance does not occur without limitations. โ€ข Based on access restrictions there are three forms: โ€“ Public โ€“ Protected โ€“ Private
  • 7. Syntax class X{ โ€ฆ }; class Y : public X{ โ€ฆ }; Class Y is derived from class X with โ€œpublicโ€ derivation
  • 8. Protected Members โ€ข Members that can be accessed within the same class and by classes derived from that class, also by friends of the class. #include <iostream> using namespace std; class A{ protected: int number; }; class B:public A{ public: void f(){number=0; cout << ++number;} }; main(){ B myb; myb.f(); }
  • 9. Example class A{ public: int a,b; }; class B : public A{ public: int b,c; }; main(){ B b; // b as object name of type B b.a=1; // a as inherited from the base class A b.b=3; // 2nd b is of derived class B b.A::b=2; // 2nd b is of base class A b.c=4; // c is of derived class B }
  • 10. Multiple Inheritance โ€ข You may need a class to inherit more than one base class. โ€ข C++ provides such flexibility with multiple inheritance โ€ข For example, a police car and an ambulance are both emergency vehicles. โ€“ Both classes may inherit from vehicle and emergency parent classes although vehicle and emergency classes are not linked to each other
  • 11. Example car emergency truck vehicle police car ambulance fire engine class vehicle {...}; class emergency {...}; class car:public vehicle {...}; class truck:public vehicle {...}; class policeCar:public car, public emergency {...}; class ambulance:public car, public emergency {...}; class fireEngine:public truck, public emergency {...};