0% found this document useful (0 votes)
7 views19 pages

Overriding Functions

The document discusses object-oriented programming techniques, focusing on function overriding, overloading, and binding concepts. It explains the differences between function overriding and overloading, the importance of virtual destructors, and the significance of static and dynamic binding in method invocation. Additionally, it highlights the benefits of dynamic binding in providing flexibility and adaptability in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views19 pages

Overriding Functions

The document discusses object-oriented programming techniques, focusing on function overriding, overloading, and binding concepts. It explains the differences between function overriding and overloading, the importance of virtual destructors, and the significance of static and dynamic binding in method invocation. Additionally, it highlights the benefits of dynamic binding in providing flexibility and adaptability in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Object Oriented

Programming Techniques
Overriding Functions
• Two classes to have a function with the same name.
• Function overriding is accomplished by using inheritance and virtual
functions.
• The concept through which we define a function in parent class and
the child class with the same return type and parameters is known as
function overriding.
• It is achieved during runtime.
Problem
Problem

Output

Only height is displayed


because cylinder class called it,
base classes display function
isn’t approached!
Overriding Functions
Solution
Output
Task
• Create two classes derive is a base class. Both the classes shows
method overriding for display function.
Overriding Functions
Task Solution

Output
Overriding Functions Vs Overloading
Function
Function Overloading Function Overriding
• Function Overloading provides multiple • Function Overriding is the redefinition
definitions of the function of base class function in its derived
• An example of compile time. class
• Overloaded functions are in same • An example of run time.
scope. • Overridden functions are in different
• Overloading is used when the same scopes.
function has to behave differently • Overriding is needed when derived
depending upon parameters passed to class function has to do some different
them. job than the base class function.
• A function has the ability to load • A function can be overridden only a
multiple times. single time.
• In function overloading, we don’t need • In function overriding, we need an
inheritance. inheritance concept.
Virtual Constructor
The creation of a virtual constructor is not possible because:

• The object is not created.


• The compiler must know the type of object before creating it.
Virtual Destructor
• We cannot delete a derived class object using a base class pointer
that has a non-virtual destructor.
• To delete the derived class object using the base class pointer, the
base class must contain a virtual destructor.
Output
Binding
Binding
• The determination of which method in the class
• hierarchy is to be invoked for a particular object.

Static (Early) Binding occurs at compile time


• When the compiler can determine which method in the class
hierarchy to use for a particular object.

Dynamic (Late) Binding occurs at run time


• When the determination of which method in the class
• Hierarchy to use for a particular object occurs during program
execution.
Static Binding
Example,
Time t1;
ExtTime et2;

t1.setTime(12, 30, 00); // static binding


et1.setExtTime(13, 45, 30); // static binding

t1.printTime( ); // static binding


et1.printTime(); // static binding
Dynamic Binding
• Binding is determined dynamically at runtime
• To indicate that a method is to be bound dynamically, the base class
must use the reserved word virtual
• When a method is defined as virtual, all overriding methods from
that point on down the hierarchy are virtual
Output
Benefits of Dynamic Binding
● Flexibility
● Using dynamic binding, programs can process different types of objects
using a common interface.
● Applications use only virtual functions defined in the base class. The
application has no knowledge about any derived class, but can generate
statements appropriate for each type of account, depending on where
base class pointers reference.
● If additional types of accounts are later added to the class hierarchy, the
application can easily generate statements appropriate to those accounts.
Static vs. Dynamic Binding

You might also like