SlideShare a Scribd company logo
Lecture 19



Public, Protected, and
  Private Inheritance
Inheritance -
                 Introduction
• Definition :Deriving methods and Data from an
  existing class to a new class .
• Use : we don’t need to redefine them in the
  new class. (Reusability)    Class A {
• Inheritance access control:         Data Members ;
                                       Member Functions
  – Private                            }
  – Protected                  Class B : public A
                                        {
  – Public                              Data Members;
                                        Member Functions
                                        }
B                  D

           Three Types of Access Control
     Suppose B is a super class and D is derived from B then

    If private inheritance, then all the members of B which are
    protected or public will be private in D.

    If protected inheritance, then all the members of B which
    are protected or public will be protected in D.

    If public inheritance, then all the members of B which are
    protected will also be protected in D and all the members of
    B which are public will also be public in D.
Private Inheritance

                     Class B
                        protected: int x
                        public: void f1()




                 Class D: private B
                     private: int y
                     public: void f2()


• int x and void f1() will be private in Class D.
If a member is private in what places it can be accessed?
            Same Class and friends of the Class
Protected Inheritance

                      Class B
                         protected: int x
                         public: void f1()




                  Class D: protected B
                      private: int y
                      public: void f2()


• int x and void f1() will be protected in Class D.
If a member is Protected in what places it can be accessed?
Same Class and friends of the Class,derived class and its friends
Public Inheritance

                    Class B
                       protected: int x
                       public: void f1()




                Class D: public B
                    private: int y
                    public: void f2()


• int x will be protected in Class D.
• void f1() will be public in Class D.
• What about Private data members ?
Sample Program
#include <iostream.h>
class Patient {                                 void InPatient::InSetdetails (int Wnum, int
    public:                                     Dys)
      void Setdetails(int, char);               { Wardnum = Wnum;
      void Displaydetails();                        Daysinward = Dys;
    private:                                    }
      int IdNumber; char Name; };
void Patient::Setdetails (int Idnum, char       void InPatient :: InDisplaydetails ()
    Namein)                                     { cout << endl << "Ward Number is "
{ IdNumber = Idnum; Name =                            << Wardnumber;
    Namein; }                                      cout << endl << "Number of days in
void Patient::Displaydetails()                  ward "
{ cout << endl << IdNumber << Name; }                 << Daysinward;
                                                }
class InPatient :     public          Patient   void main()
    {     public:                               {        InPatient p1;
          void InSetdetails (int, int);                  p1.Setdetails(1234, 'B');
          void InDisplaydetails();                       p1.Displaydetails();
                                                         p1.InSetdetails(3,14);
    private:
                                                         p1.InDisplaydetails();
          int Wardnum, Daysinward; };           }
Sample Program from Lecture 18:
            Change public to protected inheritance
#include <iostream.h>                              private:
class Patient {                                               int Wardnum, Daysinward;   };
    public:
      void Setdetails(int, char);                  void InPatient::InSetdetails (int Wnum, int
      void Displaydetails();                       Dys)
    private:                                       { Wardnum = Wnum;
      int IdNumber; char Name; };                      Daysinward = Dys;
void Patient::Setdetails (int Idnum, char          }
    Namein)                                        void InPatient :: InDisplaydetails ()
{ IdNumber = Idnum; Name = Namein; }               { cout << endl << "Ward Number is "
void Patient::Displaydetails()                           << Wardnumber;
{ cout << endl << IdNumber << Name; }                 cout << endl << "Number of days in
                                                   ward "
class InPatient : protected            Patient {         << Daysinward;
          public:                                  }
       void InSetdetails (int, int);               void main()
       void InDisplaydetails();                    {        InPatient p1;
                                                              p1.Patset();
      void Patset() { Setdetails(4321, ‘X’); }                p1.PatDisp();
      void PatDisp() { Displaydetails(); }                    p1.InSetdetails(3,14);
                                                              p1.InDisplaydetails();     }
Sample Program from Lecture 18:
             Change public to private 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 : private            Patient {              p1.Patset();
    public:                                                 p1.InSetdetails(3,14);
       void InSetdetails (int, int);                        p1.InDisplaydetails();
       void InDisplaydetails();                   }
       void Patset() { Setdetails(4321, ‘X’); }
    private:
                                                  We can access Setdetails() inside
           int Wardnum, Daysinward; };            Inpatient though it is private to
                                                  Inpatient but public to Patient.
Base-class and Derived-class
                Constructor and Destructor

• When an object of derived class is created the base class
  constructor is called first and then the derived class
  constructor is called.
  Example // Program1
• If the derived-class constructor is omitted, the derived class’s
  default constructor ( which is System Generated ) calls the
  base-class’s default constructor Program 2

• Destructors are called in the reverse order of constructor
  calls, so a derived-class destructor is called before its base-
  class destructor.
Person

   Student                                Lecturer



int main()                       Person’s object is created
{      Person Pers1;             Person’s object is created
       Student Stud1;            Student’s object is created
       Lecturer Lec1;            Person’s object is created
}                                Lecturer’s object is created
Base-class Initialiser: Sample Program 1 -
      Explicit Constructor definition
#include <iostream.h>                          void main() {
class Base {                                             Base b1;
   protected: int x, y;                                  b1.set();
   public:
     Base () {cout<<"Constructing Base                    Derived d1;
     object"<<endl;}                                      d1.set();
     ~Base() {cout<<"Destructing Base          }
     object"<<endl;}
     void set() { x = 10; y = 20;               Output:
                  cout<<x<<y<<endl;}
};                                                   Constructing Base object
class Derived : public Base {                        10 20
    private: int a, b;
    public:                                          Constructing Base object
      Derived() {cout<<"Constructing Derived         Constructing Derived object
     object"<<endl;}                                 40 60
      ~Derived() {cout<<"Destructing Derived
     object"<<endl;}                                 Destructing Derived object
      void set() { a = 40; b = 60;                   Destructing Base object
                    cout<<a<<b<<endl; }              Destructing Base object
};
Base-class Initialiser: Sample Program 1
        (No user-defined constructor for
                  derived class)
#include <iostream.h>                     void main() {
class Base {                                        Base b1;
   protected: int x, y;                             b1.set();
   public:
     Base () {cout<<"Constructing Base               Derived d1;
     object"<<endl;}                                 d1.set();
     ~Base() {cout<<"Destructing Base     }
     object"<<endl;}
     void set() { x = 10; y = 20;          Output:
                  cout<<x<<y<<endl;}
};                                              Constructing Base object
class Derived : public Base {                   10 20
    private: int a, b;
    public:                                     Constructing Base object
      void set() { a = 40; b = 60;              40 60
                    cout<<a<<b<<endl; }
};                                              Destructing Base object
                                                Destructing Base object
Base-class Initialiser



• A base-class initialiser can be provided in the derived-class
  constructor to call the base-class constructor explicitly;

• otherwise, the derived class’s constructor will call the base
  class’s default constructor implicitly.
Base-class Initialiser: Sample
                               Program 2
#include <iostream.h>                         class Circle: public Point {
class Point {                                 public:
public:                                                  Circle(double, int, int); //constructor
                                                         ~Circle();             //destructor
    Point (int, int); //constructor           private:
    ~Point();           //destructor                     double radius;
protected:                                    };
    int x, y;
                                              Circle::Circle(double r, int a, int b) : Point(a,b)
};
                                              { radius = r;
Point::Point(int a, int b)                       cout<<"Circle constructor: radius is
{ x = a; y = b;                                  "<<radius<<'['<<x<<", "<<y<<']'<<endl;
    cout<<"Point constructor: "<<'['<<x<<",   }
    "<<y<<']';
cout<<endl; }                                 Circle::~Circle()
                                              { cout<<"Circle destructor: radius is
                                                 "<<radius<<'['<<x<<", "<<y<<']'<<endl;
Point::~Point()                               }
{ cout<<"Point destructor: "<<'['<<x<<",
    "<<y<<']'; cout<<endl; }
Base-class Initialiser: Sample
                              Program 2 (cont.)
int main()                           Output:
{ {
             Point p(11, 22);            Point constructor: [11, 22]
                                         Point destructor: [11, 22]
    }
    cout<<endl;                          Point constructor: [72, 29]
    Circle circle1(4.5, 72, 29);         Circle constructor: radius is 4.5 [72, 29]
    cout<<endl;
                                         Point constructor: [5, 5]
    Circle circle2(10, 5, 5);
                                         Circle constructor: radius is 10 [5, 5]
    cout<<endl;
    return 0;                            Circle destructor: radius is 10 [5, 5]
}                                        Point destructor: [5, 5]

                                         Circle destructor: radius is 4.5 [72, 29]
                                         Point destructor: [72, 29]

More Related Content

What's hot

Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
Raghunath A
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Abu Saleh
 
Defining classes-part-i-constructors-properties
Defining classes-part-i-constructors-propertiesDefining classes-part-i-constructors-properties
Defining classes-part-i-constructors-propertiesCtOlaf
 
06 inheritance
06 inheritance06 inheritance
06 inheritance
ROHINI KOLI
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
Arslan Arshad
 
Introduction to DI(C)
Introduction to DI(C)Introduction to DI(C)
Introduction to DI(C)
Radek Benkel
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language Innovations
Shahriar Hyder
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
Aleksander Fabijan
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
Chhom Karath
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
Jason Austin
 
Java2
Java2Java2
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
Carol McDonald
 

What's hot (20)

Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
 
Defining classes-part-i-constructors-properties
Defining classes-part-i-constructors-propertiesDefining classes-part-i-constructors-properties
Defining classes-part-i-constructors-properties
 
06 inheritance
06 inheritance06 inheritance
06 inheritance
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
201005 accelerometer and core Location
201005 accelerometer and core Location201005 accelerometer and core Location
201005 accelerometer and core Location
 
Introduction to DI(C)
Introduction to DI(C)Introduction to DI(C)
Introduction to DI(C)
 
JavaYDL10
JavaYDL10JavaYDL10
JavaYDL10
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language Innovations
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
Xdebug confoo11
Xdebug confoo11Xdebug confoo11
Xdebug confoo11
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
 
Java2
Java2Java2
Java2
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
Entity api
Entity apiEntity api
Entity api
 

Viewers also liked

Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
cprogrammings
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
Anil Bapat
 

Viewers also liked (7)

Lecture4
Lecture4Lecture4
Lecture4
 
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
 
Inheritance
InheritanceInheritance
Inheritance
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similar to Lecture19

Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
instaface
 
Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
Dr. Md. Shohel Sayeed
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsPrincess Sam
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and ObjectsPayel Guria
 
Lecture4
Lecture4Lecture4
Lecture4
rajesh0ks
 
Classes and object
Classes and objectClasses and object
Classes and object
Ankit Dubey
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit Inheritance
Ananda Kumar HN
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritancePrincess Sam
 
Lecture4
Lecture4Lecture4
Lecture4
Ravi Kant Kumar
 
Inheritance
InheritanceInheritance
InheritanceTech_MX
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
abhilashagupta
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
SushmaGavaraskar
 
20070329 Object Oriented Programing Tips
20070329 Object Oriented Programing Tips20070329 Object Oriented Programing Tips
20070329 Object Oriented Programing TipsShingo Furuyama
 
Inheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybridInheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybrid
VGaneshKarthikeyan
 

Similar to Lecture19 (20)

Lecture20
Lecture20Lecture20
Lecture20
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Lecture07
Lecture07Lecture07
Lecture07
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
Lecture4
Lecture4Lecture4
Lecture4
 
Classes and object
Classes and objectClasses and object
Classes and object
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit Inheritance
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritance
 
Lecture4
Lecture4Lecture4
Lecture4
 
Inheritance
InheritanceInheritance
Inheritance
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
class and objects
class and objectsclass and objects
class and objects
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
 
Mattbrenner
MattbrennerMattbrenner
Mattbrenner
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
20070329 Object Oriented Programing Tips
20070329 Object Oriented Programing Tips20070329 Object Oriented Programing Tips
20070329 Object Oriented Programing Tips
 
Unit 4
Unit 4Unit 4
Unit 4
 
Inheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybridInheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybrid
 

More from elearning_portal (10)

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

Recently uploaded

Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
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
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
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
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Reflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdfReflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdf
amberjdewit93
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
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
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
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
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
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
 
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
 

Recently uploaded (20)

Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
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...
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Reflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdfReflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdf
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
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
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
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
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
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
 
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
 

Lecture19

  • 1. Lecture 19 Public, Protected, and Private Inheritance
  • 2. Inheritance - Introduction • Definition :Deriving methods and Data from an existing class to a new class . • Use : we don’t need to redefine them in the new class. (Reusability) Class A { • Inheritance access control: Data Members ; Member Functions – Private } – Protected Class B : public A { – Public Data Members; Member Functions }
  • 3. B D Three Types of Access Control Suppose B is a super class and D is derived from B then If private inheritance, then all the members of B which are protected or public will be private in D. If protected inheritance, then all the members of B which are protected or public will be protected in D. If public inheritance, then all the members of B which are protected will also be protected in D and all the members of B which are public will also be public in D.
  • 4. Private Inheritance Class B protected: int x public: void f1() Class D: private B private: int y public: void f2() • int x and void f1() will be private in Class D. If a member is private in what places it can be accessed? Same Class and friends of the Class
  • 5. Protected Inheritance Class B protected: int x public: void f1() Class D: protected B private: int y public: void f2() • int x and void f1() will be protected in Class D. If a member is Protected in what places it can be accessed? Same Class and friends of the Class,derived class and its friends
  • 6. Public Inheritance Class B protected: int x public: void f1() Class D: public B private: int y public: void f2() • int x will be protected in Class D. • void f1() will be public in Class D. • What about Private data members ?
  • 7. Sample Program #include <iostream.h> class Patient { void InPatient::InSetdetails (int Wnum, int public: Dys) void Setdetails(int, char); { Wardnum = Wnum; void Displaydetails(); Daysinward = Dys; private: } int IdNumber; char Name; }; void Patient::Setdetails (int Idnum, char void InPatient :: InDisplaydetails () Namein) { cout << endl << "Ward Number is " { IdNumber = Idnum; Name = << Wardnumber; Namein; } cout << endl << "Number of days in void Patient::Displaydetails() ward " { cout << endl << IdNumber << Name; } << Daysinward; } class InPatient : public Patient void main() { public: { InPatient p1; void InSetdetails (int, int); p1.Setdetails(1234, 'B'); void InDisplaydetails(); p1.Displaydetails(); p1.InSetdetails(3,14); private: p1.InDisplaydetails(); int Wardnum, Daysinward; }; }
  • 8. Sample Program from Lecture 18: Change public to protected inheritance #include <iostream.h> private: class Patient { int Wardnum, Daysinward; }; public: void Setdetails(int, char); void InPatient::InSetdetails (int Wnum, int void Displaydetails(); Dys) private: { Wardnum = Wnum; int IdNumber; char Name; }; Daysinward = Dys; void Patient::Setdetails (int Idnum, char } Namein) void InPatient :: InDisplaydetails () { IdNumber = Idnum; Name = Namein; } { cout << endl << "Ward Number is " void Patient::Displaydetails() << Wardnumber; { cout << endl << IdNumber << Name; } cout << endl << "Number of days in ward " class InPatient : protected Patient { << Daysinward; public: } void InSetdetails (int, int); void main() void InDisplaydetails(); { InPatient p1; p1.Patset(); void Patset() { Setdetails(4321, ‘X’); } p1.PatDisp(); void PatDisp() { Displaydetails(); } p1.InSetdetails(3,14); p1.InDisplaydetails(); }
  • 9. Sample Program from Lecture 18: Change public to private 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 : private Patient { p1.Patset(); public: p1.InSetdetails(3,14); void InSetdetails (int, int); p1.InDisplaydetails(); void InDisplaydetails(); } void Patset() { Setdetails(4321, ‘X’); } private: We can access Setdetails() inside int Wardnum, Daysinward; }; Inpatient though it is private to Inpatient but public to Patient.
  • 10. Base-class and Derived-class Constructor and Destructor • When an object of derived class is created the base class constructor is called first and then the derived class constructor is called. Example // Program1 • If the derived-class constructor is omitted, the derived class’s default constructor ( which is System Generated ) calls the base-class’s default constructor Program 2 • Destructors are called in the reverse order of constructor calls, so a derived-class destructor is called before its base- class destructor.
  • 11. Person Student Lecturer int main() Person’s object is created { Person Pers1; Person’s object is created Student Stud1; Student’s object is created Lecturer Lec1; Person’s object is created } Lecturer’s object is created
  • 12. Base-class Initialiser: Sample Program 1 - Explicit Constructor definition #include <iostream.h> void main() { class Base { Base b1; protected: int x, y; b1.set(); public: Base () {cout<<"Constructing Base Derived d1; object"<<endl;} d1.set(); ~Base() {cout<<"Destructing Base } object"<<endl;} void set() { x = 10; y = 20; Output: cout<<x<<y<<endl;} }; Constructing Base object class Derived : public Base { 10 20 private: int a, b; public: Constructing Base object Derived() {cout<<"Constructing Derived Constructing Derived object object"<<endl;} 40 60 ~Derived() {cout<<"Destructing Derived object"<<endl;} Destructing Derived object void set() { a = 40; b = 60; Destructing Base object cout<<a<<b<<endl; } Destructing Base object };
  • 13. Base-class Initialiser: Sample Program 1 (No user-defined constructor for derived class) #include <iostream.h> void main() { class Base { Base b1; protected: int x, y; b1.set(); public: Base () {cout<<"Constructing Base Derived d1; object"<<endl;} d1.set(); ~Base() {cout<<"Destructing Base } object"<<endl;} void set() { x = 10; y = 20; Output: cout<<x<<y<<endl;} }; Constructing Base object class Derived : public Base { 10 20 private: int a, b; public: Constructing Base object void set() { a = 40; b = 60; 40 60 cout<<a<<b<<endl; } }; Destructing Base object Destructing Base object
  • 14. Base-class Initialiser • A base-class initialiser can be provided in the derived-class constructor to call the base-class constructor explicitly; • otherwise, the derived class’s constructor will call the base class’s default constructor implicitly.
  • 15. Base-class Initialiser: Sample Program 2 #include <iostream.h> class Circle: public Point { class Point { public: public: Circle(double, int, int); //constructor ~Circle(); //destructor Point (int, int); //constructor private: ~Point(); //destructor double radius; protected: }; int x, y; Circle::Circle(double r, int a, int b) : Point(a,b) }; { radius = r; Point::Point(int a, int b) cout<<"Circle constructor: radius is { x = a; y = b; "<<radius<<'['<<x<<", "<<y<<']'<<endl; cout<<"Point constructor: "<<'['<<x<<", } "<<y<<']'; cout<<endl; } Circle::~Circle() { cout<<"Circle destructor: radius is "<<radius<<'['<<x<<", "<<y<<']'<<endl; Point::~Point() } { cout<<"Point destructor: "<<'['<<x<<", "<<y<<']'; cout<<endl; }
  • 16. Base-class Initialiser: Sample Program 2 (cont.) int main() Output: { { Point p(11, 22); Point constructor: [11, 22] Point destructor: [11, 22] } cout<<endl; Point constructor: [72, 29] Circle circle1(4.5, 72, 29); Circle constructor: radius is 4.5 [72, 29] cout<<endl; Point constructor: [5, 5] Circle circle2(10, 5, 5); Circle constructor: radius is 10 [5, 5] cout<<endl; return 0; Circle destructor: radius is 10 [5, 5] } Point destructor: [5, 5] Circle destructor: radius is 4.5 [72, 29] Point destructor: [72, 29]