Run time Polymorphism in c++

01 Mar 2022 Balmiki Mandal 0 C++

Runtime polymorphism in C++

Runtime polymorphism, also known as dynamic polymorphism or late binding, is a feature of object-oriented programming that allows objects of different types to respond to the same message in different ways. This is achieved through the use of virtual functions and function overriding.

Virtual functions are member functions that are declared as virtual in the base class and can be overridden in derived classes. When a virtual function is called on a base class object, the compiler will dynamically determine which function to call based on the actual type of the object.

Function overriding is the process of redefining a virtual function in a derived class. The overriding function may implement the same or different behavior as the base class function.

Example:

C++
class Animal {
public:
  virtual void speak() {
    std::cout << "Animal sound\n";
  }
};

class Dog : public Animal {
public:
  void speak() override {
    std::cout << "Woof!\n";
  }
};

class Cat : public Animal {
public:
  void speak() override {
    std::cout << "Meow!\n";
  }
};

int main() {
  Animal* animal = new Dog();
  animal->speak(); // Prints "Woof!"

  animal = new Cat();
  animal->speak(); // Prints "Meow!"

  delete animal;

  return 0;
}

 

 

In this example, the Animal class has a virtual function called speak(). The Dog and Cat classes override this function to provide their own implementations. When we call the speak() function on an Animal object, the compiler will dynamically determine which function to call based on the actual type of the object.

Benefits of runtime polymorphism:

  • Flexibility: Runtime polymorphism allows us to write code that is more flexible and reusable. For example, we can write a function that accepts a base class object as a parameter and calls the speak() function on it. This function will work with any object of a type that inherits from the base class, even if we don't know what type of object it is at compile time.
  • Open/closed principle: The open/closed principle states that software entities should be open for extension but closed for modification. Runtime polymorphism allows us to extend the behavior of our code without modifying existing code. For example, we can add new types of animals to our program without having to modify the speak() function.

When to use runtime polymorphism:

Runtime polymorphism is a powerful feature, but it is important to use it wisely. Overusing runtime polymorphism can lead to code that is more difficult to understand and maintain. In general, we should only use runtime polymorphism when it is necessary to achieve the desired flexibility.

Examples of runtime polymorphism in the real world:

  • A remote control: A remote control can be used to control a variety of different electronic devices, such as TVs, DVD players, and stereo systems. The remote control works by sending different signals to each device. The device then interprets the signal and takes the appropriate action.
  • A website: A website can be used by people with different types of devices, such as computers, tablets, and smartphones. The website can display different content to each device based on its capabilities.
  • A social media platform: A social media platform can be used by people from different cultures. The platform can translate the content into the user's native language.

Conclusion:

 

Runtime polymorphism is a powerful feature of object-oriented programming that allows us to write code that is more flexible and reusable. However, it is important to use runtime polymorphism wisely to avoid code that is difficult to understand and maintain.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.