What is polymorphism in C++?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Polymorphism in C++ is one of the four key principles of Object-Oriented Programming (OOP), along with encapsulation, inheritance, and abstraction. Polymorphism allows objects of different types to be treated as objects of a common base type. It enables a single function, operator, or method to work in different ways depending on the context, such as the object calling the method or the data types involved.

Types of Polymorphism in C++:

1. Compile-Time Polymorphism (Static Polymorphism)

This type of polymorphism is resolved at compile time and is typically achieved through function overloading or operator overloading. The function or operator behaves differently based on the number or type of arguments passed.

  • Function Overloading: Multiple functions with the same name but different parameter lists.

    void display(int a) { cout << "Integer: " << a << endl; } void display(double a) { cout << "Double: " << a << endl; }

    Here, display behaves differently based on the argument type.

  • Operator Overloading: Defining custom behavior for standard operators (like +, -) for user-defined types.

    class Complex { public: int real, imag; Complex operator + (const Complex& obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } };

2. Run-Time Polymorphism (Dynamic Polymorphism)

Run-time polymorphism is achieved through function overriding with virtual functions. This type of polymorphism allows derived classes to provide specific implementations of methods that are declared in a base class. The method to be called is determined at runtime based on the type of the object, not the type of the reference.

  • Virtual Functions: When a function in the base class is declared as virtual, it can be overridden by a function with the same signature in a derived class.
    class Animal { public: virtual void sound() { cout << "Some animal sound" << endl; } }; class Dog : public Animal { public: void sound() override { cout << "Bark" << endl; } }; Animal* animal = new Dog(); animal->sound(); // Outputs "Bark"

In this example, sound() behaves differently based on the actual object type (Dog) even though the pointer is of type Animal*.

Benefits of Polymorphism:

  • Code Reusability: Polymorphism allows code to be more flexible and reusable. You can write more generic functions or classes that work with multiple types or objects.
  • Extensibility: Polymorphism makes it easy to extend and maintain code. Derived classes can override base class methods, providing custom behavior while keeping a consistent interface.
  • Maintainability: Changes in one part of the code (like a base class) do not necessarily affect other parts that rely on polymorphic behavior.

Conclusion:

Polymorphism in C++ is a powerful feature that enables flexibility, code reuse, and cleaner design by allowing methods to behave differently depending on the object invoking them. Compile-time polymorphism (function/operator overloading) provides early binding, while run-time polymorphism (function overriding) supports dynamic behavior.

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is an IP address in networking?
How much Datadog interview experience is required?
How to prepare for coding interviews in shell scripting?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.