🟣 OOP  ·  Lesson 48

Virtual Functions

Virtual Functions

What is Virtual Functions?

Virtual Functions
A virtual function enables run-time polymorphism. When a base class pointer points to a derived object, the derived version is called.
Level
🟣 Object-Oriented Programming
Example File
virtual-functions.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
virtualVirtual used in Virtual Functions programming.
overridingOverriding used in Virtual Functions programming.
base pointerBase pointer used in Virtual Functions programming.
dynamic bindingDynamic binding used in Virtual Functions programming.

Syntax / Pattern

virtual void functionName();

Example Program

#include <iostream>
using namespace std;
class Shape{ public: virtual void draw(){ cout << "Shape"; } };
class Circle: public Shape{ public: void draw(){ cout << "Circle"; } };
int main(){ Shape *s = new Circle(); s->draw(); delete s; }

Expected Output

Circle

Program Explanation

  • draw() is virtual in base class.
  • Base pointer calls derived Circle version.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • graphics
  • plugin systems
  • flexible OOP design

Common Mistakes

  • Forgetting virtual when using base pointer.
  • Not making base destructor virtual in polymorphic classes.

Practice Tasks

  1. Create Animal sound example.
  2. Explain dynamic binding.

Summary

Virtual Functions is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

What is Virtual Functions?

Virtual Functions
A virtual function enables run-time polymorphism. When a base class pointer points to a derived object, the derived version is called.
Level
🟣 Object-Oriented Programming
Example File
virtual-functions.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
virtualVirtual used in Virtual Functions programming.
overridingOverriding used in Virtual Functions programming.
base pointerBase pointer used in Virtual Functions programming.
dynamic bindingDynamic binding used in Virtual Functions programming.

Syntax / Pattern

virtual void functionName();

Example Program

#include <iostream>
using namespace std;
class Shape{ public: virtual void draw(){ cout << "Shape"; } };
class Circle: public Shape{ public: void draw(){ cout << "Circle"; } };
int main(){ Shape *s = new Circle(); s->draw(); delete s; }

Expected Output

Circle

Program Explanation

  • draw() is virtual in base class.
  • Base pointer calls derived Circle version.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • graphics
  • plugin systems
  • flexible OOP design

Common Mistakes

  • Forgetting virtual when using base pointer.
  • Not making base destructor virtual in polymorphic classes.

Practice Tasks

  1. Create Animal sound example.
  2. Explain dynamic binding.

Summary

Virtual Functions is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

← Back to C++ Tutorial