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

C++ Polymorphism का परिचय

Polymorphism का मतलब है एक command object के आधार पर अलग तरीकों से काम कर सकता है, जैसे speak एक dog को bark कराए और एक cat को meow। आप अलग चीज़ों को same तरीके से treat कर सकते हैं।
Syntax
cpp
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?

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

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

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

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

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

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

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

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

Polymorphism के Benefits

Polymorphism ही object-oriented code को extensible बनाता है: आप बाद में existing code की एक भी line छुए बिना बिल्कुल नई derived classes add कर सकते हैं जो already base class pointers के जरिए काम करती है, क्योंकि उस code को कभी specific subtypes के बारे में पहली जगह जानने की ज़रूरत नहीं थी।

उदाहरण: Benefits of Polymorphism

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 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;
}
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 method पर virtual भूल जाना, ताकि base pointers के जरिए base version हमेशा चले।
  2. overloading (same नाम, अलग parameters) को overriding (एक derived class में same signature) से confuse करना।
  3. एक base pointer या reference की बजाय एक base object पर method call करना और polymorphic 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.