Skip to main content
Topic- Virtual Function and
Function Overloading
Group Activity
Prepared by group 3
Course: Object Oriented Programming with C+
+
Course Code: CSE2001
SLOT: A21+A22+A23
Fall Semester 2025-26
Team Members:
BHARGAV MAKWANA
24BCY10224
24BCY10289
RADHEY MOHAN SINGH 24BCY10066
ARIHANT VATS
AKSHIT BAKSHI
ANIMESH TIWARI
ANANYA IYER
VENISHA VERMA
DIVYANSH MAURYA
24BCY10256
24BCY10315
24BCY10356
24BCY10151
24BCY10130
Concept of Virtual Function:
• Definition
: A virtual function is a member function in a base class declared with the
virtual keyword, designed to be overridden in derived classes to enable
dynamic (runtime) polymorphism.
• Importance in
OOP:
Virtual functions support runtime polymorphism, allowing objects of
different derived classes to be treated uniformly through base class
pointers or references while invoking the correct overridden function.
⚬ Flexibility: Enables writing generic code that works with different derived
types without modification.
⚬ Extensibility: New derived classes can be added with specific behaviors
without changing existing code, promoting maintainability and scalability.
• Use of virtual
Keyword:
Declaring a function as virtual in the base class signals the compiler
to perform dynamic dispatch, ensuring that the derived class’s
version of the function is called at runtime, not the base class’s.
• Need for Runtime
Polymorphism:
Working of Virtual Function:
A virtual function is a base-class member
marked with the keyword virtual to allow
derived classes to override it and enable
run-time (dynamic) polymorphism.
The call resolves based on the actual object
type (derived class) even when accessed
through a base pointer/reference, ensuring
correct behavior with heterogeneous
objects.
Compilers typically implement virtual
dispatch via a per-class vtable and a per-
object hidden pointer (v ptr) that selects the
correct function at run time.
Late binding: Virtual member calls are
resolved at run time by vtable lookup, so
base-pointer calls invoke the derived
override corresponding to the object’s
dynamic type.
V-Table and
V-Pointer
Concept
• When a class has at least one virtual
function, compilers typically generate a
per-class virtual table (vtable) containing
function pointers to the class’s virtual
functions.
• Each polymorphic object stores a hidden
v-pointer (vptr) that points to its class’s
vtable; constructors set the vptr so that
it refers to the most-derived class’s
vtable during each construction phase.
Example: Base pointer →
Derived object.:-
Code:-
Output:-
Here we can see the working of virtual function with
example. Virtual keyword is used in base class
function and a base pointer is created. Overriding is
implemented successfully with help of virtual
keyword.
Rules of Virtual Function:
• Virtual functions must be declared with the virtual keyword in the
base class.
• Derived classes can override (redefine) the virtual function to provide
specific behavior.
• Virtual functions are accessed through base class pointers or
references to enable dynamic dispatch.
Declaration-
Redefinition-
Access-
Polymorphism-
• Calling a virtual function via a base pointer/reference invokes the
derived class’s version at runtime.
Destructor-
• Base class destructors should be virtual if the class is intended for
inheritance to ensure proper cleanup.
Syntax of Virtual Function:
#include <iostream>
using namespace std;
class Base {
public:
virtual return_type
functionName(parameter_list) {
// base class implementation (optional)
}
};
int main( )
{ }
Example of declaration only:
class Base {
public:
virtual void show() {
// Base class
implementation
std::cout << "Base show
function" << std::endl;
}
};
Example of Virtual
Function::-
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() { cout << "Base displayn"; }
};
class Derived : public Base {
public:
void display() override { cout << "Derived displayn"; }
};
int main() {
Base* ptr = new Derived();
ptr->display(); // Outputs: Derived display
delete ptr;
return 0;
• }
• Allow runtime polymorphism (decides at run time which func
to call).
• Flexible – derived classes can change base class methods.
• Help in code reusability (use base class pointer/reference).
• Make programs more extensible (easy to add new classes).
• Support OOP concepts like inheritance and abstraction.
Advantages of Virtual Function:
Limitations of Virtual Function:
• Slightly slower execution because of extra lookup.
• Takes a bit more memory (vtable is stored).
• If not overridden, may give unexpected output.
• Can be hard to underand with deep inheritance.
• Do not work inside constructors.
Concept of Funtion Overloading:
1.Same function name, different
signatures
• The signature of a function includes the
number and types of its parameters.
• The return type alone cannot differentiate
overloaded functions.
🔑 Key Points:
Function overloading is a feature in
many object-oriented programming
languages (like C++ and Java) that
allows you to define multiple
functions with the same name but
with different parameter lists
(number of parameters, types of
parameters, or both).
Concept-
1.Improves code clarity
• Instead of giving different names
for similar operations (e.g.,
addInt, addFloat), you can just
use add().
Example:-
#include <iostream>
using namespace std;
class Math {
public:
// Function to add two integers
int add(int a, int b) {
return a + b;
}
// Function to add two doubles
double add(double a, double b) {
return a + b;
}
// Function to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
};
int main() {
Math obj;
cout << obj.add(5, 10) << endl; // Calls int version
cout << obj.add(3.5, 2.5) << endl; // Calls double version
cout << obj.add(1, 2, 3) << endl; // Calls three-parameter version
return 0;
}
Rules Of Function Overloading in
C++
• Same function name, different parameter
list
Function name must differ by number of
parameters , type of parameters, or order of
parameters.
• Return type alone cannot overload a
function
You cannot create to functions with the same name
and same parameters but only different return types
• Default arguments may cause
ambiguity
Overloaded functions should avoid default arguments
that confuse the compiler
• Overloading is resolved at compile
time
This is an example of compile time
polymorphism.
Syntax of Function
Overloading:
#include <iostream>
using namespace std;
class xyz
{ public:
returnType
FunctionName(parameter_List1);
{ }
returnType
FunctionName(parameter_List2);
{ }
returnType
FunctionName(parameter_List3);
{ }
Example of declaration only:
int add(int a, int b);
// 2 int parameters
int add( int a ,int b, int c);
// 3 int parameters
double add ( double x, double
y);
// 2 double parameters
Example of Function
Overloading
Example of Function
Overloading
• Code readability
• Code reusability
• Abstraction
• Simplifies function naming
• Enhances maintainability
Advantages of Function Overloading:
Limitations of Function Overloading:
• Compiler confusion
• Ambiguity errors
• Requires distinct parameter lists
• Overload resolution complexity
• Mathematical operations (e.g., add, multiply)
• Constructors with different parameters
• Input/output functions
• Utility functions handling multiple data types
Practical Use:
• Virtual functions enable dynamic method dispatch at runtime,
supporting polymorphic behavior in inheritance hierarchies.
• Function overloading allows multiple functions with the same
name but different parameters, resolved at compile time.
Comparison between
Virtual Function and Function Overloading:
Example of Virtual
Function:-
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base show" << endl;
}
};
class Derived : public Base {
public:
void show() override {
cout << "Derived show" << endl;
}
};
int main() {
Base* ptr = new Derived();
ptr->show(); // Calls Derived's show() due to virtual function
delete ptr;
return 0;
• }
Explanation:
show() is declared virtual in Base.
Derived overrides show().
Accessing through Base* pointer calls the Derived
version at runtime.
Example of Function
Overloading:-
#include <iostream>
using namespace std;
void print(int x) {
cout << "Integer: " << x << endl;
}
void print(double x) {
cout << "Double: " << x << endl;
}
int main() {
print(5); // Calls print(int)
print(3.14); // Calls print(double)
return 0;
}
Explanation:
Two print functions with different parameter
types.
Compiler decides which function to call based
on argument type at compile time.
Conclusion
Virtual functions enable runtime polymorphism, allowing derived classes to provide specific
implementations that are invoked through base class pointers or references. This supports
dynamic behavior and is essential for achieving flexible and extensible object-oriented
designs.
Function overloading provides compile-time polymorphism by allowing multiple functions
with the same name but different parameter lists. It improves code readability and usability
by enabling functions to handle different types or numbers of arguments.
Both concepts enhance the flexibility of C++ programs but operate at different stages:
virtual functions resolve calls at runtime, while function overloading resolves them at
compile time. Understanding their differences is key to effective C++ programming and
designing robust, maintainable software.
Thank
you