Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Understanding Virtual

Functions in C++ OOP


Group Name: Meghna

Ishrak Ahmed ID: 20234103050


Md.Shaiful Islam Akash ID: 20234103025
Md Shihab Uddin ID: 20234103048
Md. Sajeeb Islam ID: 20234103077
Maisha Akter Bonna ID: 20234103042
Almas Sheikh ID: 20234103068
Maria Mastura Ushno ID: 20234103070
Tasnia Hossain ID: 20234103162
Abu Sufian Utsha ID: 20234103057
Abu Nayeem Riyad (c) ID: 20234103055
Introduction
• Definition of virtual functions:
Virtual functions are functions in base
class that are overridden in derived
class. They enable polymorphic
behavior.
• Importance in Object-Oriented
Programming (OOP): Virtual
functions allow for dynamic binding
and facilitate code reuse and
extensibility.
Polymorphism
• Polymorphism: The ability of objects to
take on multiple forms. In C++, this is
achieved through function overloading and
overriding.

• How virtual functions enable


polymorphic behavior: Virtual
functions allow a derived class to provide a
specific implementation of a function while
still adhering to the interface defined in
the base class.
Virtual Functions and
Inheritance

• Relationship between virtual


functions and inheritance: Virtual
functions enable polymorphic behavior
across inheritance hierarchies. They allow
derived classes to provide specific
implementations while maintaining a
common interface.
• How virtual functions are utilized in
inheritance hierarchies: Virtual
functions are declared in the base class
and overridden in derived classes to
provide specialized behavior.
Syntax of declaring virtual functions:
Example code snippet demonstrating a virtual function:
Dynamic Binding

• Explanation of dynamic binding:


Dynamic binding, also known as late
binding, occurs when the function call is
resolved at runtime based on the actual
type of the object.
• How virtual functions achieve
dynamic binding at runtime:
When a virtual function is called through a
base class pointer or reference, the
appropriate derived class function is
invoked based on the actual type of the
object being pointed to or referenced.
Early binding

Output::
Late binding

Output::
Pure Virtual Functions

• Definition of pure virtual


functions: Pure virtual functions are
virtual functions that have no
implementation in the base class. They
must be overridden by derived classes.

• Pure virtual functions are used to define


abstract base classes, which cannot be
instantiated and serve as interfaces for
derived classes.
Example of pure virtual function

Output:
Benefits of Virtual
Functions

• Flexibility in design: Virtual functions


allow for flexibility in designing class
hierarchies and enable polymorphic
behavior.
• Enhanced code readability and
maintainability: By observing to a
common interface, virtual functions
improve code readability and make it
easier to extend and maintain software
systems.
Performance
Considerations

• Overhead associated with virtual


function calls: Virtual function calls incur
a slight performance overhead due to
dynamic binding at runtime.

• Trade-offs between flexibility and


performance: While virtual functions
offer flexibility, excessive use may impact
performance. It's important to strike a
balance between flexibility and
performance based on the specific
requirements of the application.
Best Practices

• When to use virtual functions:


Use virtual functions when you need
polymorphic behavior and want to provide
a common interface for derived classes.

• Guidelines for effective use:


Design class hierarchies carefully, favoring
composition over inheritance where
appropriate, and consider the trade-offs
between flexibility and performance.
Example Application

•Showcase a real-world scenario where


virtual functions are beneficial: For
example, in a graphics application, virtual
functions can be used to define a common
sound interface with sound() method,
allowing various sound like tiger’s,
monkey’s, and Cow’s to provide their
specific implementation of Sounds of
Different Animals.
Real world Example

Output:
Output:
Key Takeaways
• Virtual functions enable dynamic
polymorphism in C++.

• They allow for code reuse, extensibility,


and maintainability.

• Remember to mark base class functions as


virtual and override them in derived
classes for desired behavior.
Conclusion

• Importance of virtual functions


in C++ OOP: Virtual functions are a
fundamental feature of C++ OOP,
allowing for code reuse, extensibility,
and efficient design of complex
systems.
Thank You
Q&A

You might also like