Lecture19

533 views

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
533
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
78
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Lecture19

  1. 1. Lecture 19Public, Protected, and Private Inheritance
  2. 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. 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. 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. 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. 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. 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 invoid 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. 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. 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. 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. 11. Person Student Lecturerint 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. 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 objectclass 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. 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 objectclass 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. 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. 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. 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]

×