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.