SlideShare a Scribd company logo
Lecture 18



Inheritance: Base and
  Derived Classes
Introduction

• In dictionary inheritance is defined as the action of inheriting;
  the transfer of property; to receive from a predecessor.
• In programming language term, inheritance refers to objects
  inheriting properties(data members & member functions) of
  another class.
• Advantage: code reusability.
• Definition: Inheritance is the mechanism which allows a
  class A to inherit properties of a class B. We say "A inherits
  from B". Objects of class A thus have access to attributes
  and methods of class B without having to redefine them.
• Definition: If class A inherits from class B then B is called
  the superclass (or parent class) of A. A is called the subclass
  (or child class) of B.
Introduction


            Class Person
            Data: x
            Method: f1()




 Class Student         Class Employee
 Data: y               Data: z
 Method: f2()          Method: f3()



Class Undergraduate
Data: a
Method: f4()
Single Inheritance

• A class is derived from ONE base class.

                   Class Patient
                   Data: Idnum, Name
                   Method: SetDetails(),
                           DisplayDetails()




                 Class InPatient
                 Data: WardNum,
                       DaysinWard
                 Method: InSetDetails(),
                          InDisplayDetails()
Sample Program of Single Inheritance

#include <iostream.h>                        void InPatient::InSetdetails (int Wnum, int Dys)
class Patient {                              { Wardnum = Wnum;
    public:                                      Daysinward = Dys;
       void Setdetails(int, char);           }
       void Displaydetails();
    private:                                 void InPatient :: InDisplaydetails ()
       int IdNumber; char Name; };           { cout << endl << "Ward Number is "
void Patient::Setdetails (int Idnum, char          << Wardnumber;
    Namein)                                     cout << endl << "Number of days in ward "
{ IdNumber = Idnum; Name = Namein; }               << Daysinward;
void Patient::Displaydetails()               }
{ cout << endl << IdNumber << Name; }        void main()
                                             {         InPatient p1;
class InPatient : public Patient { public:             p1.Setdetails(1234, 'B');
           void InSetdetails (int, int);               p1.Displaydetails();
           void InDisplaydetails();                    p1.InSetdetails(3,14);
                                                       p1.InDisplaydetails();
    private:                                 }
           int Wardnum, Daysinward; };
Multiple Inheritance

• A class is derived from more than one base classes.

  Class Physical                      Class Mental
  Data: Height,                       Data: IQ, Readingage
        Weight                        Method: SetMental(),
  Method: SetPhysical(),                       DisplayMental()
           DisplayPhysica ()



                      Class Person
                      Data: Name
                      Method: SetName()
Sample Program of Multiple
                              Inheritance
#include <iostream.h>                       class Person : public Physical , public Mental
class Physical {                            { private:
    private :                                       char Name;
                                               public:
           float height, weight;
                                                   void setname()
    public :                                       { cin >> Name; }
       void setphysical()                   };
        { cin >> height; cin >> weight; }
       void displayphysical()               void main ()
                                            {          Person a1;
        { cout << height << weight; } };
                                                       a1.setname();
class Mental {                                         a1.setphysical();
     private :                                         a1.setmental();
           int IQ, Readingage;                         a1.displayphysical();
     public :                                          a1.displaymental();
                                            }
       void setmental()
        { cin >> IQ; cin >> Readingage; }
       void displaymental()
        { cout << IQ << Readingage; } };
Access Control


• If a member is declared in a class C and is private, it can
  only be used by the member functions in C and by the
  friends of class C.


   Class C                          Class E: friend Class C
   private: int a;                  private: int num;
   public: void Set_a()             public: void Set_num()


• void Set_a() and Class E can access the private data
  member, a which belongs to Class C.
Access Control

• If a member is declared in a class C and the member is
  protected, it can only be used by the member functions in
  C, friends of C and member functions and friends of
  classes derived from C.
  Class C                             Class E: friend Class C
  protected: int a;                   private: int num;
  public: void Set_a()                public: void Set_num()


  Class D                             Class F: friend Class D
  private: int numD;                  private: int numF;
  public: void Set_numD()             public: void Set_numF()

 • void Set_a(),Class E, Class D, and Class F can access the private
   data member, a which belongs to Class C.
Access Control

• If a member is public it can be used everywhere without
  restrictions.

  Class C
  public: int a;
  public: void Set_a()

• int a and void Set_a can be accessed everywhere.
Access Control

• A derived class cannot access directly the private members
  of its base class.

• However, the derived class can access the public and
  protected member of its base class.

• The derived class can only access private members of the
  base class only through access functions provided in the
  base class’s public and protected interfaces.

More Related Content

What's hot

Inheritance
InheritanceInheritance
Inheritance
Jancirani Selvam
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
ProfSonaliGholveDoif
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
Ameen Sha'arawi
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritance
zindadili
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Laxman Puri
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
Nilesh Dalvi
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
Sebastian Marek
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
Sujan Mia
 
Inheritance
InheritanceInheritance
Inheritance
Burhan Ahmed
 
Lecture4
Lecture4Lecture4
Lecture4
rajesh0ks
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Majid Saeed
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritanceharshaltambe
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop InheritanceHadziq Fabroyir
 
Core java oop
Core java oopCore java oop
Core java oop
Parth Shah
 

What's hot (20)

Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
class and objects
class and objectsclass and objects
class and objects
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 
Core java oop
Core java oopCore java oop
Core java oop
 

Viewers also liked

Personal development plan, realising your potential
Personal development plan, realising your potentialPersonal development plan, realising your potential
Personal development plan, realising your potential
Mat Tinker
 
My personal development plan
My personal development planMy personal development plan
My personal development plankarinairina
 
Personal Development Plan 2014 - 2015
Personal Development Plan 2014 - 2015Personal Development Plan 2014 - 2015
Personal Development Plan 2014 - 2015Zeal Liew
 
THE BENEFITS OF HAVING PERSONAL DEVELOPMENT PLAN
THE BENEFITS OF HAVING PERSONAL DEVELOPMENT PLANTHE BENEFITS OF HAVING PERSONAL DEVELOPMENT PLAN
THE BENEFITS OF HAVING PERSONAL DEVELOPMENT PLAN
Adina Nikmawati
 
PDP Your personal development plan
PDP Your personal development planPDP Your personal development plan
PDP Your personal development plan
Datio Big Data
 
Personal Development Plan & Mentoring
Personal Development Plan & MentoringPersonal Development Plan & Mentoring
Personal Development Plan & Mentoring
IngeborgWerther
 
Maintenance management in operations management
Maintenance management in operations managementMaintenance management in operations management
Maintenance management in operations management
Shereen Shahana
 
Standards in facility management
Standards in facility managementStandards in facility management
Standards in facility management
Global Business Intelligence
 
Individual Development Plan - David Penwell
Individual Development Plan - David PenwellIndividual Development Plan - David Penwell
Individual Development Plan - David PenwellDavid Penwell
 
Personal Development Plans
Personal Development PlansPersonal Development Plans
Personal Development Plans
Making Business Matter Ltd
 
2014 personal development ppt
2014 personal development ppt2014 personal development ppt
2014 personal development ppt
Bill Schult
 
Wk 1 personal development plan
Wk 1   personal development planWk 1   personal development plan
Wk 1 personal development plan
wtcelearning
 
Individual development plan
Individual development planIndividual development plan
Individual development plan
Seta Wicaksana
 
Personal Development
Personal DevelopmentPersonal Development
Personal Development
Seta Wicaksana
 

Viewers also liked (17)

Personal development plan, realising your potential
Personal development plan, realising your potentialPersonal development plan, realising your potential
Personal development plan, realising your potential
 
My personal development plan
My personal development planMy personal development plan
My personal development plan
 
Personal Development Plan 2014 - 2015
Personal Development Plan 2014 - 2015Personal Development Plan 2014 - 2015
Personal Development Plan 2014 - 2015
 
THE BENEFITS OF HAVING PERSONAL DEVELOPMENT PLAN
THE BENEFITS OF HAVING PERSONAL DEVELOPMENT PLANTHE BENEFITS OF HAVING PERSONAL DEVELOPMENT PLAN
THE BENEFITS OF HAVING PERSONAL DEVELOPMENT PLAN
 
PDP Your personal development plan
PDP Your personal development planPDP Your personal development plan
PDP Your personal development plan
 
Personal Development Plan & Mentoring
Personal Development Plan & MentoringPersonal Development Plan & Mentoring
Personal Development Plan & Mentoring
 
Maintenance management in operations management
Maintenance management in operations managementMaintenance management in operations management
Maintenance management in operations management
 
Standards in facility management
Standards in facility managementStandards in facility management
Standards in facility management
 
Individual Development Plan - David Penwell
Individual Development Plan - David PenwellIndividual Development Plan - David Penwell
Individual Development Plan - David Penwell
 
Fm ppt 1
Fm ppt 1Fm ppt 1
Fm ppt 1
 
Personal Development Plans
Personal Development PlansPersonal Development Plans
Personal Development Plans
 
2014 personal development ppt
2014 personal development ppt2014 personal development ppt
2014 personal development ppt
 
Wk 1 personal development plan
Wk 1   personal development planWk 1   personal development plan
Wk 1 personal development plan
 
Individual development plan
Individual development planIndividual development plan
Individual development plan
 
Personal Development
Personal DevelopmentPersonal Development
Personal Development
 
My Personal Development Plan
My Personal Development PlanMy Personal Development Plan
My Personal Development Plan
 
Career plan example
Career plan exampleCareer plan example
Career plan example
 

Similar to Lecture18

Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
Dr. Md. Shohel Sayeed
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
RutujaTandalwade
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
Amritsinghmehra
 
Inheritance
InheritanceInheritance
InheritanceTech_MX
 
class c++
class c++class c++
class c++
vinay chauhan
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
abhilashagupta
 
Inheritance
InheritanceInheritance
Inheritance
GowriLatha1
 
Lecture4
Lecture4Lecture4
Lecture4
Ravi Kant Kumar
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
irshadkumar3
 
Inheritance
InheritanceInheritance
Inheritance
Mustafa Khan
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
BArulmozhi
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
Mohamad Al_hsan
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and ObjectsPayel Guria
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
ArafatSahinAfridi
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
nafisa rahman
 

Similar to Lecture18 (20)

Lecture21
Lecture21Lecture21
Lecture21
 
Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
class c++
class c++class c++
class c++
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Lecture4
Lecture4Lecture4
Lecture4
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 

More from elearning_portal (10)

Lecture05
Lecture05Lecture05
Lecture05
 
Lecture17
Lecture17Lecture17
Lecture17
 
Lecture16
Lecture16Lecture16
Lecture16
 
Lecture10
Lecture10Lecture10
Lecture10
 
Lecture06
Lecture06Lecture06
Lecture06
 
Lecture20
Lecture20Lecture20
Lecture20
 
Lecture03
Lecture03Lecture03
Lecture03
 
Lecture02
Lecture02Lecture02
Lecture02
 
Lecture01
Lecture01Lecture01
Lecture01
 
Lecture04
Lecture04Lecture04
Lecture04
 

Recently uploaded

Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
ArianaBusciglio
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
NelTorrente
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
MERN Stack Developer Roadmap By ScholarHat PDF
MERN Stack Developer Roadmap By ScholarHat PDFMERN Stack Developer Roadmap By ScholarHat PDF
MERN Stack Developer Roadmap By ScholarHat PDF
scholarhattraining
 
Reflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPointReflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPoint
amberjdewit93
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Reflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdfReflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdf
amberjdewit93
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptxFresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
SriSurya50
 

Recently uploaded (20)

Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
MERN Stack Developer Roadmap By ScholarHat PDF
MERN Stack Developer Roadmap By ScholarHat PDFMERN Stack Developer Roadmap By ScholarHat PDF
MERN Stack Developer Roadmap By ScholarHat PDF
 
Reflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPointReflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPoint
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Reflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdfReflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdf
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptxFresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
 

Lecture18

  • 1. Lecture 18 Inheritance: Base and Derived Classes
  • 2. Introduction • In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive from a predecessor. • In programming language term, inheritance refers to objects inheriting properties(data members & member functions) of another class. • Advantage: code reusability. • Definition: Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say "A inherits from B". Objects of class A thus have access to attributes and methods of class B without having to redefine them. • Definition: If class A inherits from class B then B is called the superclass (or parent class) of A. A is called the subclass (or child class) of B.
  • 3. Introduction Class Person Data: x Method: f1() Class Student Class Employee Data: y Data: z Method: f2() Method: f3() Class Undergraduate Data: a Method: f4()
  • 4. Single Inheritance • A class is derived from ONE base class. Class Patient Data: Idnum, Name Method: SetDetails(), DisplayDetails() Class InPatient Data: WardNum, DaysinWard Method: InSetDetails(), InDisplayDetails()
  • 5. Sample Program of Single Inheritance #include <iostream.h> void InPatient::InSetdetails (int Wnum, int Dys) class Patient { { Wardnum = Wnum; public: Daysinward = Dys; void Setdetails(int, char); } void Displaydetails(); private: void InPatient :: InDisplaydetails () int IdNumber; char Name; }; { cout << endl << "Ward Number is " void Patient::Setdetails (int Idnum, char << Wardnumber; Namein) cout << endl << "Number of days in ward " { IdNumber = Idnum; Name = Namein; } << Daysinward; void Patient::Displaydetails() } { cout << endl << IdNumber << Name; } void main() { InPatient p1; class InPatient : public Patient { public: p1.Setdetails(1234, 'B'); void InSetdetails (int, int); p1.Displaydetails(); void InDisplaydetails(); p1.InSetdetails(3,14); p1.InDisplaydetails(); private: } int Wardnum, Daysinward; };
  • 6. Multiple Inheritance • A class is derived from more than one base classes. Class Physical Class Mental Data: Height, Data: IQ, Readingage Weight Method: SetMental(), Method: SetPhysical(), DisplayMental() DisplayPhysica () Class Person Data: Name Method: SetName()
  • 7. Sample Program of Multiple Inheritance #include <iostream.h> class Person : public Physical , public Mental class Physical { { private: private : char Name; public: float height, weight; void setname() public : { cin >> Name; } void setphysical() }; { cin >> height; cin >> weight; } void displayphysical() void main () { Person a1; { cout << height << weight; } }; a1.setname(); class Mental { a1.setphysical(); private : a1.setmental(); int IQ, Readingage; a1.displayphysical(); public : a1.displaymental(); } void setmental() { cin >> IQ; cin >> Readingage; } void displaymental() { cout << IQ << Readingage; } };
  • 8. Access Control • If a member is declared in a class C and is private, it can only be used by the member functions in C and by the friends of class C. Class C Class E: friend Class C private: int a; private: int num; public: void Set_a() public: void Set_num() • void Set_a() and Class E can access the private data member, a which belongs to Class C.
  • 9. Access Control • If a member is declared in a class C and the member is protected, it can only be used by the member functions in C, friends of C and member functions and friends of classes derived from C. Class C Class E: friend Class C protected: int a; private: int num; public: void Set_a() public: void Set_num() Class D Class F: friend Class D private: int numD; private: int numF; public: void Set_numD() public: void Set_numF() • void Set_a(),Class E, Class D, and Class F can access the private data member, a which belongs to Class C.
  • 10. Access Control • If a member is public it can be used everywhere without restrictions. Class C public: int a; public: void Set_a() • int a and void Set_a can be accessed everywhere.
  • 11. Access Control • A derived class cannot access directly the private members of its base class. • However, the derived class can access the public and protected member of its base class. • The derived class can only access private members of the base class only through access functions provided in the base class’s public and protected interfaces.