SlideShare uma empresa Scribd logo
1 de 11
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.

Mais conteúdo relacionado

Mais procurados

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 inheritancezindadili
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh 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 PythonSujith 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
 
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
 
#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
 

Mais procurados (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
 

Destaque

Personal development plan, realising your potential
Personal development plan, realising your potentialPersonal development plan, realising your potential
Personal development plan, realising your potentialMat 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 PLANAdina Nikmawati
 
PDP Your personal development plan
PDP Your personal development planPDP Your personal development plan
PDP Your personal development planDatio Big Data
 
Personal Development Plan & Mentoring
Personal Development Plan & MentoringPersonal Development Plan & Mentoring
Personal Development Plan & MentoringIngeborgWerther
 
Maintenance management in operations management
Maintenance management in operations managementMaintenance management in operations management
Maintenance management in operations managementShereen Shahana
 
Individual Development Plan - David Penwell
Individual Development Plan - David PenwellIndividual Development Plan - David Penwell
Individual Development Plan - David PenwellDavid Penwell
 
2014 personal development ppt
2014 personal development ppt2014 personal development ppt
2014 personal development pptBill Schult
 
Wk 1 personal development plan
Wk 1   personal development planWk 1   personal development plan
Wk 1 personal development planwtcelearning
 
Individual development plan
Individual development planIndividual development plan
Individual development planSeta Wicaksana
 

Destaque (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
 

Semelhante a Lecture18

Semelhante a 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
 

Mais de 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
 

Último

The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 

Último (20)

The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

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.