SlideShare ist ein Scribd-Unternehmen logo
1 von 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.

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie Lecture18

Ähnlich wie 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
 

Mehr von 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
 

Kürzlich hochgeladen

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 

Kürzlich hochgeladen (20)

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 

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.