What is virtual function and why is it used? with example.
What is virtual function and why is it used? with example.
Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Anubhav Kumar
06-Nov-2025Why it is used
Virtual functions are used to:
Syntax
In C++:
Example — Without and With Virtual Function
Without virtual function:
Output:
With virtual function:
Output:
Key Points:
virtualkeyword.overridekeyword (C++11 onward) ensures that the function actually overrides a base class virtual function.