C++ में Runtime Polymorphism
In this page:
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
// 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
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
virtualभूल जाना, ताकि pointer के जरिए base version call हो।- एक derived object को एक base object को value से assign करना, जो derived हिस्सा slice कर देता है और polymorphism खो देता है।
- एक base pointer के जरिए delete करते समय एक virtual destructor भूल जाना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: