You are on page 1of 3

Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows

objects of different classes to be treated as objects of a common superclass. It enables code to be


written in a more generic and flexible manner, promoting reusability and extensibility.
Polymorphism allows for the implementation of methods in different ways based on the types of
objects they operate on, leading to more modular and maintainable code.

There are two main types of polymorphism: compile-time polymorphism (also known as static
polymorphism) and runtime polymorphism (also known as dynamic polymorphism).

1. Compile-Time Polymorphism: Compile-time polymorphism is achieved through


function overloading and operator overloading. In function overloading, multiple
functions with the same name but different parameter lists can be defined within the same
scope. The compiler determines which function to call based on the number and types of
arguments provided. This allows for the creation of multiple versions of a function
tailored to different types of input.

cpp

#include <iostream>

class Math {

public:

// Function overloading

int add(int a, int b) {

return a + b;

double add(double a, double b) {

return a + b;

}
};

int main() {

Math math;

std::cout << math.add(1, 2) << std::endl; // Calls int version

std::cout << math.add(1.5, 2.5) << std::endl; // Calls double version

return 0;

 In this example, the add method is overloaded to accept both integers and doubles as
arguments. Depending on the type of arguments provided, the appropriate version of the add
method is called.

Operator overloading, as demonstrated earlier, is another form of compile-time polymorphism. It


allows operators such as +, -, *, /, etc., to be redefined for user-defined types, enabling natural
syntax for performing operations on objects.

 Runtime Polymorphism: Runtime polymorphism is achieved through virtual functions and


inheritance. Virtual functions are functions declared within a base class that can be overridden in
derived classes. When a virtual function is called through a base class pointer or reference, the
actual function to be called is determined at runtime based on the type of the object it points to or
refers to.

#include <iostream>

class Animal {

public:

// Virtual function

virtual void makeSound() {

std::cout << "Animal makes a sound" << std::endl;


}

};

class Dog : public Animal {

public:

// Override the makeSound function

void makeSound() override {

std::cout << "Dog barks" << std::endl;

};

int main() {

Animal* animal = new Dog(); // Polymorphic behavior

animal->makeSound(); // Calls Dog's makeSound

delete animal;

return 0;

In this example, the Animal class defines a virtual function makeSound, which is overridden in
the Dog class. When a Dog object is accessed through an Animal pointer, the makeSound function
of the Dog class is called at runtime, demonstrating polymorphic behavior.

Polymorphism is a key concept in OOP that enhances code flexibility, extensibility, and
maintainability. It allows for code to be written in a more generic and reusable manner, leading
to more modular and scalable software systems. By leveraging polymorphism, developers can
write code that is easier to understand, maintain, and evolve over time.

You might also like