C++ के Virtual Functions
In this page:
class Base {
public:
virtual return_type method_name() { }
};
class Derived : public Base {
public:
return_type method_name() override { }
};
Virtual Functions का Introduction
एक virtual function virtual keyword इस्तेमाल करके base class में declared एक member function है, जो compiler को बताता है कि कौन सा version call करना है यह decision runtime तक defer करे, pointer या reference के declared type की बजाय object के actual type के आधार पर।
उदाहरण: Introduction to Virtual Functions
// Include std::cout and std::cin
#include <iostream>
// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
virtual void speak() { std::cout << "Animal" << std::endl; }
};
// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
void speak() override { std::cout << "Dog" << std::endl; }
};
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
Animal *a = new Dog();
a->speak();
// Free the heap memory with delete
delete a;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
override Keyword
C++11 ने override keyword introduce किया, जिसे आप अपना इरादा explicit बनाने के लिए एक derived class के overriding method पर लिखते हैं।
Compiler फिर verify करता है कि यह method वाकई एक parent class में एक virtual function से exactly match करता है, typos या signature mismatches पकड़ते हुए जो अन्यथा चुपचाप एक नया, unrelated function बना देते।
उदाहरण: The override Keyword
// Include std::cout and std::cin
#include <iostream>
// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
virtual void speak() { std::cout << "Animal" << std::endl; }
};
// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
void speak() override { std::cout << "Dog" << std::endl; }
};
// Program execution starts in main()
int main() {
Dog d;
d.speak();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Virtual Destructors
अगर आप एक base class pointer के जरिए एक derived-class object delete करें, derived class का destructor तब तक नहीं चलेगा जब तक base class का destructor virtual mark न हो — यह skip करना resource leaks का एक classic source है, क्योंकि derived destructor की कोई भी cleanup logic बस कभी execute नहीं होती।
उदाहरण: Virtual Destructors
// Include std::cout and std::cin
#include <iostream>
// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
virtual ~Animal() { std::cout << "Animal destroyed" << std::endl; }
};
// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
~Dog() { std::cout << "Dog destroyed" << std::endl; }
};
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
Animal *a = new Dog();
// Free the heap memory with delete
delete a;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Virtual Functions का Internal Working
अंदर से, C++ VTABLE नाम की function pointers की एक hidden table इस्तेमाल करके dynamic dispatch implement करता है, हर object के अंदर एक hidden pointer के साथ जिसे vptr कहते हैं जो इसकी class की table की ओर point करता है।
यह machinery automatically चलती है और सिर्फ एक छोटी, predictable runtime cost add करती है।
उदाहरण: Internal Working of Virtual Functions
#include <iostream>
class Animal {
public:
virtual void speak() { std::cout << "Animal" << std::endl; }
// each object carries a hidden vptr into its class's VTABLE
};
class Dog : public Animal {
public:
void speak() override { std::cout << "Dog" << std::endl; }
};
int main() {
Animal *a = new Dog();
a->speak();
delete a;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Virtual Functions के Rules
Virtual functions कुछ hard rules के साथ आते हैं: वे कभी static नहीं हो सकते, क्योंकि static functions किसी particular object से tied नहीं होते और dynamic dispatch को एक चाहिए, और एक overriding function का return type या तो exactly match होना चाहिए या एक compatible covariant type होना चाहिए।
उदाहरण: Rules of Virtual Functions
#include <iostream>
class Animal {
public:
virtual void speak() { std::cout << "Animal" << std::endl; }
// virtual functions can never be static
};
class Dog : public Animal {
public:
void speak() override { std::cout << "Dog" << std::endl; }
};
int main() {
Animal *a = new Dog();
a->speak();
delete a;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- base function में
virtualभूल जाना, ताकि runtime पर derived version choose न हो। overrideन लिखना, ताकि एक mismatched signature बिना किसी warning के एक नए function की तरह compile हो जाए।- destructor virtual न होने पर एक base pointer के जरिए एक derived object delete करना, जो undefined behavior है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: