SlideShare una empresa de 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.

Más contenido relacionado

La actualidad más candente

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 objects
Payel Guria
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
harshaltambe
 
#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
Hadziq Fabroyir
 

La actualidad más candente (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
 

Destacado

My personal development plan
My personal development planMy personal development plan
My personal development plan
karinairina
 
Personal Development Plan 2014 - 2015
Personal Development Plan 2014 - 2015Personal Development Plan 2014 - 2015
Personal Development Plan 2014 - 2015
Zeal Liew
 
Individual Development Plan - David Penwell
Individual Development Plan - David PenwellIndividual Development Plan - David Penwell
Individual Development Plan - David Penwell
David Penwell
 

Destacado (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 a Lecture18

Inheritance
InheritanceInheritance
Inheritance
Tech_MX
 
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
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
Payel Guria
 

Similar 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
 

Más 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

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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
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
QucHHunhnh
 

Último (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
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
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
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
 

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.