Introduction

Hello, learner! In today's exciting chapter, we will explore Polymorphism, a prominent feature of Object-Oriented Programming (OOP). Specifically, we will study its role in maintaining backward compatibility while introducing new features. Think of it as a software update that introduces new functions without breaking the older functionality — ingenious, isn't it?

Understanding Polymorphism

Polymorphism, a principle that derives from the Greek words 'poly' (many) and 'morphism' (forms), enables a function or class in C++ to handle different types and forms. In C++, polymorphism is often achieved through the use of virtual functions and method overriding.

Consider a base class Bird with a virtual method fly(). If we create derived classes like Sparrow, Penguin, and Ostrich, we can override the fly() method for certain subclasses. This demonstrates polymorphism in action.

#include <iostream>
using namespace std;

class Bird {  // Base class
public:
    virtual string canFly() {
        return "Unknown";
    }
};

class Sparrow : public Bird {  // Derived class
public:
    string canFly() override {
        return "Yes, I can fly!";
    }
};

class Penguin : public Bird {  // Derived class
public:
    string canFly() override {
        return "No, I prefer swimming.";
    }
};

int main() {
    Sparrow sparrow;
    Penguin penguin;
    cout << "Sparrow says: " << sparrow.canFly() << endl;  // Output: "Yes, I can fly!"
    cout << "Penguin says: " << penguin.canFly() << endl;  // Output: "No, I prefer swimming."
    return 0;
}
Polymorphism for Backward Compatibility

When adding new features that introduce new behaviors to some components, polymorphism ensures that the existing parts function as before, thereby retaining backward compatibility. C++ achieves this through virtual functions and method overloading.

For example, consider a MathOperations class with a multiply() method that accepts two parameters. To support the multiplication of three numbers, create an ExtendedMathOperations class that inherits from MathOperations and includes a new multiply() method.

#include <iostream>
using namespace std;

class MathOperations {  // Base class
public:
    virtual int multiply(int a, int b) {
        return a * b;
    }
};

class ExtendedMathOperations : public MathOperations {  // Derived class
public:
    int multiply(int a, int b) override {
        return MathOperations::multiply(a, b);  // Call the base class version
    }

    int multiply(int a, int b, int c) {
        return a * b * c;
    }
};

int main() {
    MathOperations mathOps;
    ExtendedMathOperations extendedMathOps;
    cout << mathOps.multiply(2, 3) << endl;  // Output: 6
    cout << extendedMathOps.multiply(2, 3) << endl;  // Output: 6, keeping backward compatibility
    cout << extendedMathOps.multiply(2, 3, 4) << endl;  // Output: 24
    return 0;
}
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal