0% found this document useful (0 votes)
2 views

EE172_Lecture_5 inheritance and polymorphism

This document covers the fundamentals of Object Oriented Programming (OOP), focusing on key concepts such as inheritance, polymorphism, and the various types of inheritance in C++. It explains how inheritance allows one class to acquire properties from another, and details the different access modes and types of inheritance like single, multilevel, multiple, hierarchical, and hybrid. Additionally, it discusses polymorphism, including function and operator overloading, and function overriding.

Uploaded by

regan12francis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

EE172_Lecture_5 inheritance and polymorphism

This document covers the fundamentals of Object Oriented Programming (OOP), focusing on key concepts such as inheritance, polymorphism, and the various types of inheritance in C++. It explains how inheritance allows one class to acquire properties from another, and details the different access modes and types of inheritance like single, multilevel, multiple, hierarchical, and hybrid. Additionally, it discusses polymorphism, including function and operator overloading, and function overriding.

Uploaded by

regan12francis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

EE172

Computer Programming for


Engineers

Lecture 5:
Object Oriented Programming (OOP)
1
What is OOP

Pillars of OOP

Classes and Objects

Inheritance

Polymorphism
2
Inheritance
3

 Inheritance is the capability of one class to acquire properties


and characteristics from another class
 The class whose properties are inherited by other class is called
the Parent or Base or Super class.
 The class which inherits properties of other class is called Child
or Derived or Sub class.
 Inheritance makes code reusable. When we inherit an existing
class, all its methods and fields become available in the new
class, hence code is reused.
Inheritance…
4

 A Child class inherits all public and protected members of a Parent


class.
 Private members of a parent class are never inherited.

 Basic syntax of inheritance:

 Access Mode specifies, the mode in which the properties of superclass


will be inherited into subclass (public, private, protected)
Inheritance…
5

Base ANIMAL Animal class has one


class (4 legs) attribute (4 legs)

Derived DOG A Dog class will have


class (1 tail) 4 legs and 1 tail
Example of Inheritance

Base class Animal

Derived class Dog


inheriting Animal
class

Dog object contains all


features from both
Animal and Dog classes
6
Private members cannot be Inherited!!

Base class Animal


with private data

Dog class inheriting


Animal class

Animal::legs
cannot be
inherited!!
7
Inheritance (Access Modes)
8

 Public inheritance: Makes public members of the base class


public in the derived class, and the protected members of the
base class remain protected in the derived class.
 Protected inheritance: Makes both public and protected
members of the base class protected in the derived class.
 Private inheritance: Makes both public and protected
members of the base class private in the derived class.
Inheritance (Access Modes)
Base class

Public stays public


Protected stays protected

All members from base class


become protected

All members from Base class


become private
9
Types of Inheritance in C++

10
Single Inheritance
11

 Single inheritance: inheritance in which a derived class


inherits from only one base class.

Base Class

Derived Class
Single Inheritance

Base class Animal

Derived class Dog


class inherits from
Animal class

12
Multilevel Inheritance
13

 Multilevel inheritance: inheritance in which a derived class


inherits from another derived class
Base Class A

Derived Class B

Derived Class C
Multilevel Inheritance

Base class Animal

Derived class Dog


inherits from Animal

Derived class Bulldog


inherits from Dog

14
Multiple Inheritance
15

 Multiple inheritance: a new class inherits the attributes from


two or more classes

B-1 B-2 B-3

Derived Class C
Multiple Inheritance

Base class Animal

Base class Dog

Derived class
Bulldog inherits from
Animal and Dog

16
Hierarchical Inheritance
17

 Hierarchical inheritance: more than one class is derived


from a single class

Base Class

D-1 D-2 D-3


Hierarchical Inheritance

Base class Cats

Derived class Tiger

Derived class Cheetah

18
Hybrid Inheritance
19

 Hybrid inheritance: a combination of more than one type of


inheritance
Base Class
Hierarchical
Inheritance
D-1 D-2

Multiple
inheritance
D-3
What is OOP

Pillars of OOP

Classes and Objects

Inheritance

Polymorphism
20
Polymorphism
21

 The word Polymorphism means having many forms.

 Polymorphism means that multiple functions or operators share the


same name, but performing different functionalities in different contexts.

 Typically, polymorphism occurs when there is a hierarchy of classes


which are related by inheritance.

 Inheritance lets us inherit attributes and methods from another class.

 Polymorphism uses those methods to perform different tasks in differet


ways
Polymorphism…
22

 We can implement Polymorphism using the following ways:

Function overloading
Operator overloading
Function overriding
Virtual functions
Function Overloading
23

 In C++, we can use two functions having the same name if they have
different parameters (either types or number of arguments).
 Depending upon the number/type of arguments, the same function
can perform the task in different ways
Function Overloading

Sum of two integers

Sum of three
integers

Sum of double
numbers

24
Operator Overloading
25

 We can redefine or overload most of the built-in operators available


in C++ to perform different operations on user-defined data types.
 For example, the ‘+’ operator is used for adding two numbers.
 We can overload this operator in String class to concatenate two
strings by just using +.

=9

= ABCDEF
Operator Overloading

Adding two
numbers

Join two strings

26
Function Overriding
27

 In C++ inheritance, we can have the same function in the base class
as well as its derived classes
 Function overriding occurs when a derived class has the same
definition of a functions from the base class.
 Therefore the base function is said to be overridden.

Base class
sum(int a, int b)

Derived class
sum (int a, int b)
Function Overriding

Base class with


one function

Derived class
with the same
function

28
29

You might also like