🟣 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 Functions programming में Virtual use होता है। |
| overriding | Virtual Functions programming में Overriding use होता है। |
| base pointer | Virtual Functions programming में Base pointer use होता है। |
| dynamic binding | Virtual Functions programming में Dynamic binding use होता है। |
Syntax / Pattern
C++
virtual void functionName();
Example Program
main.cpp
#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 एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।
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.cppमुख्य फोकस
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
इसे क्यों सीखें?
- यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
- यह school practicals, projects और competitive programming में बार-बार use होता है।
- यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।
ज़रूरी Terms
| Term | अर्थ / उपयोग |
|---|---|
| virtual | Virtual Virtual Functions programming में use होता है। |
| overriding | Overriding Virtual Functions programming में use होता है। |
| base pointer | Base pointer Virtual Functions programming में use होता है। |
| dynamic binding | Dynamic binding Virtual Functions programming में use होता है। |
Syntax / Pattern
C++
virtual void functionName();
Example Program
main.cpp
#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.
सारांश
Virtual Functions एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.