← Back to C++ Course | Chapter 10: Polymorphism & Abstraction | Lesson 3 of 11

C++ के Virtual Functions

एक virtual function program चलते हुए एक child के version को चुनने देता है, भले ही आप object को इसके parent की तरह hold करें, एक animal पर speak call करने और सही sound पाने जैसा। यह flexible designs को power देता है।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
#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;
}

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

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. base function में virtual भूल जाना, ताकि runtime पर derived version choose न हो।
  2. override न लिखना, ताकि एक mismatched signature बिना किसी warning के एक नए function की तरह compile हो जाए।
  3. destructor virtual न होने पर एक base pointer के जरिए एक derived object delete करना, जो undefined behavior है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.