🟣 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.
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
🟣 Object-Oriented Programming
Example File
virtual-functions.cppMain Focus
Concept + syntax + practical C++ program
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
| Term | Meaning / Use |
|---|---|
| virtual | Virtual used in Virtual Functions programming. |
| overriding | Overriding used in Virtual Functions programming. |
| base pointer | Base pointer used in Virtual Functions programming. |
| dynamic binding | Dynamic 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
- Create Animal sound example.
- 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.
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
🟣 Object-Oriented Programming
Example File
virtual-functions.cppMain Focus
Concept + syntax + practical C++ program
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
| Term | Meaning / Use |
|---|---|
| virtual | Virtual used in Virtual Functions programming. |
| overriding | Overriding used in Virtual Functions programming. |
| base pointer | Base pointer used in Virtual Functions programming. |
| dynamic binding | Dynamic 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
- Create Animal sound example.
- 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.