C++ Polymorphism का परिचय
In this page:
class Base {
public:
virtual void method_name() { }
};
class Derived : public Base {
public:
void method_name() override { }
};
Polymorphism क्या है?
Polymorphism, literally 'many forms', एक single action को उस object के actual type के आधार पर अलग तरीके से perform होने देता है, इसलिए same function call caller को यह जाने बिना कि उसके पास किस तरह का है अलग तरह के objects के लिए अलग behavior produce कर सकता है।
उदाहरण: What is Polymorphism?
// 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 << "..." << 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 << "Woof" << 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
Polymorphism के Types
C++ दोनों static (compile-time) polymorphism support करता है, function और operator overloading से achieved, और dynamic (runtime) polymorphism, virtual functions से achieved — दोनों इसमें अलग हैं कि कौन सा function call करना है यह decision actually कब लिया जाता है।
उदाहरण: Types of Polymorphism
// Include std::cout and std::cin
#include <iostream>
void show(int x) { std::cout << "int: " << x << std::endl; }
void show(double x) { std::cout << "double: " << x << std::endl; }
// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
virtual void speak() { std::cout << "Animal" << std::endl; }
};
// Program execution starts in main()
int main() {
show(5);
show(5.5);
Animal a;
a.speak();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Compile-Time Polymorphism
Compile-time polymorphism program के actually चलने से पहले completely compiler द्वारा resolve होता है, purely function names और argument types के आधार पर, जो इसे runtime पर essentially free बनाता है क्योंकि program execute होते समय लेने के लिए कोई decision नहीं बचता।
उदाहरण: Compile-Time Polymorphism
// Include std::cout and std::cin
#include <iostream>
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << add(2, 3) << std::endl;
// Print to the console with cout
std::cout << add(2.5, 3.5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Runtime Polymorphism Concept
Runtime polymorphism आपको एक parent pointer या reference के against code लिखने देता है जबकि जो actual method execute होती है वह dynamically determine होती है, call होने पर pointer जिस object की ओर point कर रहा है उसके real (derived) type के आधार पर।
उदाहरण: Runtime Polymorphism Concept
// 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; }
};
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
Shape *s = new Circle();
s->draw();
// Free the heap memory with delete
delete s;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Polymorphism के Benefits
Polymorphism ही object-oriented code को extensible बनाता है: आप बाद में existing code की एक भी line छुए बिना बिल्कुल नई derived classes add कर सकते हैं जो already base class pointers के जरिए काम करती है, क्योंकि उस code को कभी specific subtypes के बारे में पहली जगह जानने की ज़रूरत नहीं थी।
उदाहरण: Benefits of Polymorphism
// 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 Triangle
class Triangle : public Shape {
// Members below can be accessed from outside the class
public:
void draw() override { std::cout << "Triangle" << std::endl; }
};
void render(Shape *s) { s->draw(); }
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
Shape *t = new Triangle();
render(t);
// Free the heap memory with delete
delete t;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- base method पर
virtualभूल जाना, ताकि base pointers के जरिए base version हमेशा चले। - overloading (same नाम, अलग parameters) को overriding (एक derived class में same signature) से confuse करना।
- एक base pointer या reference की बजाय एक base object पर method call करना और polymorphic behavior की उम्मीद करना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: