Module 5.1 Inheritance
Module 5.1 Inheritance
EEC-101
Programming with C++
Module-5:
Object Oriented Design:
About Subject
• Object Oriented Programming Concepts
– Inheritance and composition;
– Dynamic binding and virtual functions;
– Polymorphism;
– Dynamic data in classes.
2
Inheritance
• The reuse of a class that has already been tested, debugged, and used many
times can save the effort of developing and testing the same again.
• C++ strongly supports the concept of reusability
• Once a class is written and tested, it can be adapted by other programmers to suit
their requirements.
• This can be done by creating new classes, reusing the properties of the existing
ones.
• The mechanism of deriving a new class from an old one is called inheritance (or
derivation).
3
Base and Derived Class
• The old class is referred to as the base class.
• The derived class inherits some /all the traits from the base class.
• A class can also inherit properties from more than one class or from
more than one level.
4
Types of Inheritance
1. A derived class with only one base class, is called single inheritance.
3. Traits of one class may be inherited by more than one class. This process is
referred to as hierarchical inheritance.
5. Where two or more types of inheritance are applied to design a program is called hybrid
inheritance.
5
Types of Inheritance
6
Defining Derived Classes
• A derived class can be defined by
specifying its relationship with the base
class in addition to its own details.
• colon indicates that the derived-class-name
Syntax: is derived from the base class
• visibility-mode is optional, by default it is
class derived-class-name : visibility-mode private.
base-class-name • if present, it may be public, protected, or
{ private
……………
…………… // members of the derived class
……………
}
7
Example:
class ABC: private XYZ //private derivation
{
members of ABC
}; (in all the cases, XYZ is the base
class and ABC is the derived class)
class ABC: public XYZ //public derivation by
{
members of ABC
};
8
• When a base class is privately inherited by a derived class:
– public members of the base class become private members of the derived
class
9
• When the base class is publicly inherited:
– public members of the base class become public members of the derived class
10
Single Inheritance
11
Example
12
• Private member of a base class cannot be inherited, and therefore it is not available for
the derived class directly.
• This requirement can be accomplished by modifying the visibility limit of the private
member by making it public.
• However, this would make it accessible to all other functions of the program, taking away
the advantage of data hiding.
13
Protected
• A member declared as protected is accessible by the member functions within its
class and any class immediately derived from it.
14
• A class can use all the three visibility modes as shown in the example:
class A
{
private:
………. // optional
………. // visible to member functions within its class
protected:
………..
………... //visible to member functions of its own and derived class
public:
………….
…………. // visible to all functions in the program
};
15
• When a protected member is inherited in public mode, it becomes protected in
the derived class, too, and, therefore, is accessible by the member functions of the
derived class.
16
• Protected member inherited in private mode derivation, becomes private in the
derived class
17
• It is also possible to inherit a base class in protected mode, called protected
derivation
• In protected derivation, both public and protected members of the base class
become protected members of the derived class
18
Visibility of Inherited members
Access mode in Base Class Inherited as: Resulting Access mode in
class Derived class
Public Public
Public
Protected Protected
Private Not Inherited
Public Protected
Protected Protected Protected
Private Not Inherited
Public Private
Protected Private Private
Private Not Inherited
19
Base Class Derived Class visibility
Visibility
20
Base Derived Class visibility
Class
Visibility
Public derivation Private derivation
Private Not inherited Not inherited
Protected Protected Private
Public Public Private
21
Reasoning for the Previous Example
22
Reasoning for the Previous Example
23
Example
24
Access control to private and protected
• The friend function and member function of a friend class can have direct
access to private and protected data
25
• Member functions of the derived class can directly access only the protected and
public data
• They can access the private data through the member functions (public) of the
base class
26