Lecture18
Upcoming SlideShare
Loading in...5
×
 

Lecture18

on

  • 243 views

 

Statistics

Views

Total Views
243
Views on SlideShare
243
Embed Views
0

Actions

Likes
1
Downloads
16
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Lecture18 Lecture18 Presentation Transcript

  • Lecture 18Inheritance: 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 UndergraduateData: aMethod: 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 Mentalclass 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.