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

C++ में Runtime Polymorphism

Runtime polymorphism का मतलब है program चलते हुए यह चुनता है कि किस version का function चलाना है, real object के आधार पर। यह एक remote control की तरह है जो हर device के लिए सही चीज़ करता है।
Syntax
cpp
Base *pointer_name = new Derived();
pointer_name->virtual_method();  // calls Derived's version at runtime
delete pointer_name;

Runtime Polymorphism समझाया गया

Runtime polymorphism एक C++ program को, actually चलते हुए, decide करने देता है कि कौन सा specific function implementation execute करना है — एक behavior जिसे late binding या dynamic binding कहा जाता है, उन decisions के contrast में जो compiler पहले से ले सकता है।

उदाहरण: Runtime Polymorphism Explained

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

Base Class Pointers इस्तेमाल करना

अलग-अलग derived types के objects को refer करने के लिए base class pointers इस्तेमाल करना आपको कई अलग concrete components को एक single, unified interface से handle करने देता है, calling code को यह जानने या परवाह करने की ज़रूरत के बिना कि यह exactly किस derived type के साथ काम कर रहा है।

उदाहरण: Using Base Class Pointers

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
	virtual void draw() { std::cout << "Shape" << std::endl; }
};

// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
	void draw() override { std::cout << "Circle" << std::endl; }
};

class Square : public Shape {
public:
	void draw() override { std::cout << "Square" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Shape *shapes[2] = {new Circle(), new Square()};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 2; i++) shapes[i]->draw();
	return 0;
}

Base References इस्तेमाल करना

एक parent-class reference parameter accept करने वाले functions को derived objects pass करना polymorphic code लिखने का एक खासकर clean तरीका है, क्योंकि यह null-checking और explicit pointer syntax avoid करता है जो base-pointer-based polymorphism को चाहिए।

उदाहरण: Using Base References

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

void makeSpeak(Animal &a) {
	a.speak();
}

// Program execution starts in main()
int main() {
	Dog d;
	makeSpeak(d);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Runtime बनाम Compile-time Performance

Dynamic binding runtime पर कौन सा function call करना है resolve करने के लिए ज़रूरी VTABLE lookup से एक छोटी performance cost introduce करता है, लेकिन बदले में यह substantial structural flexibility खरीदता है — नए derived types को बाद में बिना किसी existing polymorphic code बदले add किया जा सकता है।

उदाहरण: Runtime vs Compile-time Performance

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

Real-world Polymorphic Design

Real-world software समय के साथ extensible रहने के लिए runtime polymorphism पर heavily depend करता है — payment processors जो नए payment methods support करते हैं, या messaging systems जो नए transport protocols support करते हैं, दोनों आमतौर पर एक polymorphic base type के around बनाए जाते हैं।

उदाहरण: Real-world Polymorphic Design

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type PaymentMethod
class PaymentMethod {
// Members below can be accessed from outside the class
public:
	virtual void pay() { std::cout << "Generic payment" << std::endl; }
};

// Define the type CreditCard
class CreditCard : public PaymentMethod {
// Members below can be accessed from outside the class
public:
	void pay() override { std::cout << "Paid with credit card" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	PaymentMethod *method = new CreditCard();
	method->pay();
	// Free the heap memory with delete
	delete method;
	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. virtual भूल जाना, ताकि pointer के जरिए base version call हो।
  2. एक derived object को एक base object को value से assign करना, जो derived हिस्सा slice कर देता है और polymorphism खो देता है।
  3. एक base pointer के जरिए delete करते समय एक virtual destructor भूल जाना।

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.