You are on page 1of 8

TOPIC-5

OVERLOADING AND
OVERRIDING
OVERLOADING INTRODUCTION
 One of the more powerful features for code readability
and usability is that of overloading.
 Like most things, it can be used for both good and evil.

 Function overloading is the availability of various


functions within a class that differ from each other in
function signature i.e. various functions share same
name with different parameter types or number of
parameters.
FUNCTIONS IN C++
 A function in C++ is not uniquely identified by its name
alone.
 Each function has a function signature, consisting of
two elements.
 The name of the method

 The order and type of its parameters.

 Together, these act as the unique identifier for a C++


method.
 The following thus are two different functions:

addTwo (int x, int y);


addTwo (float x, float y);
FUNCTION OVERLOADING

 The process of providing more than one functions with the


same name is called method overloading.
 We say that these functions have been overloaded.

 Overloading makes sure that we can provide a consistent and


clear interface to our methods regardless of the parameters
type.
FUNCTION OVERLOADING
 The compiler works out which of the methods to call based
on the parameters it is passed.
 It will check for the method that has a matching signature.

 It will execute that method only.

 If no matching signatures are found, a compile-time error


will be displayed.
FUNCTION OVERRIDING
 A function in child class overrides a function in parent
class if they have the same name and type signature.
 Classes in which functions are defined must be in a
parent-child relationship.
 Overloading deals with multiple functions in the same
class with the same name but different signatures
 Overriding deals with two functions, one in a parent
class and one in a child class, that have the same
signature

You might also like