0% found this document useful (0 votes)
4 views26 pages

Module 5.1 Inheritance

The document provides an overview of Object Oriented Programming concepts in C++, focusing on inheritance, its types, and visibility modes. It explains how derived classes can inherit properties from base classes, the implications of public, protected, and private inheritance, and the access control for inherited members. Additionally, it discusses the importance of reusability in programming through inheritance.

Uploaded by

betzayda40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views26 pages

Module 5.1 Inheritance

The document provides an overview of Object Oriented Programming concepts in C++, focusing on inheritance, its types, and visibility modes. It explains how derived classes can inherit properties from base classes, the implications of public, protected, and private inheritance, and the access control for inherited members. Additionally, it discusses the importance of reusability in programming through inheritance.

Uploaded by

betzayda40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

INDIAN INSTITUTE OF TECHNOLOGY ROORKEE

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.

Inheritance, when used to modify and extend the


capabilities of the existing classes, becomes a very
powerful tool for incremental program development.

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 new one is called the derived class or subclass.

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

2. The mechanism of deriving a class from another ‘derived class’ is called


multilevel inheritance.

3. Traits of one class may be inherited by more than one class. This process is
referred to as hierarchical inheritance.

4. A derived class with several base classes is called multiple 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
};

class ABC: XYZ //private derivation by default


{
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

– Therefore, they are accessible to the objects of the derived class

• Private members are not inherited

• And therefore, private members of a base class will never become


members of its 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.

• C++ provides a third visibility modifier, protected

13
Protected
• A member declared as protected is accessible by the member functions within its
class and any class immediately derived from it.

• It cannot be accessed by the functions outside these two classes.

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.

• It is also ready for further inheritance.

16
• Protected member inherited in private mode derivation, becomes private in the
derived class

• Although it is available to the member functions of the derived class, it is not


available for further inheritance.

• Since private members cannot be inherited.

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

Public derivation Private derivation


Private Not inherited Not inherited
Protected Protected Private
Public Public Private

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

• Functions that can have access to these members are :


1. A function that is a friend of the class
2. A member function of a class that is a friend of the class
3. A member function of a derived class

• 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

You might also like