Skip to content

Polymorphism in CPP

Carolina Daniel edited this page Aug 22, 2023 · 1 revision

What is Polymorphism?

Polymorphism is a fundamental concept in object-oriented programming that allows objects of different classes to be treated as if they are objects of a common base class. This enables the creation of more flexible and modular code by promoting code reusability and flexibility in handling different types of objects through a common interface.

Polymorphism comes in two main flavors: compile-time (static) polymorphism and runtime (dynamic) polymorphism.

Ways to Implement Polymorphism in C++

Compile-Time Polymorphism

  • Function Overloading: This occurs when multiple functions with the same name but different parameter lists are defined in a class. The appropriate function is called based on the arguments passed during compilation.
class MathOperations {
public:
    int add(int a, int b);
    double add(double a, double b);
};
  • Operator Overloading: C++ allows operators to be redefined for user-defined types. This enables custom behavior for operators when used with objects of a class.
class Complex {
public:
    Complex operator+(const Complex& other);
};

Runtime Polymorphism

  • Virtual Functions: Virtual functions are used to achieve runtime polymorphism. A base class declares a function as virtual, and derived classes can provide their own implementation. The function to be executed is determined during runtime based on the actual object type.
class Shape {
public:
    virtual double area() const;
};

class Circle : public Shape {
public:
    double area() const override;
};

class Square : public Shape {
public:
    double area() const override;
};
  • Function Pointers and Virtual Tables: Behind the scenes, compilers use function pointers and virtual tables to implement dynamic dispatch. Virtual tables store pointers to virtual functions of classes. This enables the selection of the appropriate function to call at runtime.

Abstract Classes

An abstract class is a class that cannot be instantiated on its own but serves as a base for other classes. It contains at least one pure virtual function (a function with no implementation). Abstract classes define a common interface that derived classes must implement, ensuring a consistent behavior across a hierarchy of classes.

class Animal {
public:
    virtual void makeSound() = 0; // Pure virtual function
};

class Dog : public Animal {
public:
    void makeSound() override {
        cout << "Woof!" << endl;
    }
};

class Cat : public Animal {
public:
    void makeSound() override {
        cout << "Meow!" << endl;
    }
};

Interfaces

In C++, interfaces are not a distinct language feature like in some other programming languages. Instead, they are typically achieved through abstract classes with only pure virtual functions. An interface defines a contract that classes must adhere to by providing implementations for all the functions declared in the interface.

class Printable {
public:
    virtual void print() const = 0;
};

class Book : public Printable {
public:
    void print() const override {
        cout << "Printing book details..." << endl;
    }
};

class Magazine : public Printable {
public:
    void print() const override {
        cout << "Printing magazine details..." << endl;
    }
};

Polymorphism is a powerful concept in C++ that enables code to be more flexible and adaptable by allowing objects of different types to be treated uniformly through a common interface. It can be achieved through compile-time polymorphism with function overloading and operator overloading, as well as runtime polymorphism with virtual functions and dynamic dispatch. Abstract classes and interfaces further enhance the ability to design modular and extensible code.

Clone this wiki locally