🟣 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 Functions programming में Virtual use होता है।
overridingVirtual Functions programming में Overriding use होता है।
base pointerVirtual Functions programming में Base pointer use होता है।
dynamic bindingVirtual 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

  1. Create Animal sound example.
  2. 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.
Level
🟣 Object-Oriented Programming
Example File
virtual-functions.cpp
मुख्य फोकस
Concept + syntax + practical C++ program

इसे क्यों सीखें?

  • यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
  • यह school practicals, projects और competitive programming में बार-बार use होता है।
  • यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।

ज़रूरी Terms

Termअर्थ / उपयोग
virtualVirtual Virtual Functions programming में use होता है।
overridingOverriding Virtual Functions programming में use होता है।
base pointerBase pointer Virtual Functions programming में use होता है।
dynamic bindingDynamic 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)

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

सारांश

Virtual Functions एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।

← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n

💻 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ें.