SlideShare uma empresa Scribd logo
1 de 16
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]

Mais conteúdo relacionado

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

Destaque

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
 

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

Semelhante a Lecture 19 - Three Types of Inheritance Access Control

Semelhante a Lecture 19 - Three Types of Inheritance Access Control (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
 

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

Último

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
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
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
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
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
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 ReformChameera Dedduwage
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Último (20)

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
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"
 
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
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
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
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
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
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

Lecture 19 - Three Types of Inheritance Access Control

  • 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]