Location>code7788 >text

What is Polymorphism? An Object-Oriented Understanding of Polymorphism

Popularity:766 ℃/2024-10-30 22:16:06

This article originally came from:What is Polymorphism? An Object-Oriented Understanding of Polymorphism

What is Polymorphism

Polymorphism is a core concept in object-oriented programming (OOP) that allows objects to take on multiple forms. Polymorphism allows the same interface to be used for different data types, thus making the code more flexible and extensible.

Simply put, polymorphism is an interface, a class, an abstract class, and the methods inside a class, the same method in different classes, can have multiple implementations, and this corresponds to the specific ways of inheritance, overloading, and rewriting inside object-oriented.

Advantages of polymorphism Advantages:

  • Flexibility: Polymorphism allows the same interface to be used for different objects, thus making the code more flexible.
  • Extensibility: you can extend the functionality of the program by adding new classes without modifying the existing code.
  • Code reuse: With polymorphism, it is possible to write more generic and reusable code.

Polymorphism is an important feature in object-oriented programming that allows objects to appear in multiple forms, thus making code more flexible and extensible. Different polymorphic behaviors can be achieved through compile-time polymorphism (e.g., function overloading and operator overloading) and run-time polymorphism (e.g., virtual functions and interfaces).

Types of Polymorphism

There are two main types of polymorphisms:

  • Compile-time polymorphism (static polymorphism):
    • Function Overloading (Function Overloading): the same function name can have different parameter lists, thus realizing different functions.
    • Operator Overloading: allows the user to define or redefine the behavior of operators.
  • Runtime polymorphism (dynamic polymorphism):
    • Virtual Functions (Virtual Functions): dynamic binding is achieved by calling functions of derived classes through base class pointers or references.
    • Interfaces and Abstract Classes: Uniform interfaces are defined through interfaces or abstract classes, and different classes can implement these interfaces, thus achieving polymorphism.

Examples of compile-time polymorphism

function overloading

#include <iostream>

class Print {
public:
    void show(int i) {
        std::cout << "Integer: " << i << std::endl;
    }

    void show(double d) {
        std::cout << "Double: " << d << std::endl;
    }

    void show(const std::string& s) {
        std::cout << "String: " << s << std::endl;
    }
};

int main() {
    Print p;
    (5); // exports: Integer: 5
    (3.14); // exports: Double: 3.14
    ("Hello"); // exports: String: Hello

    return 0;
}

Operator Overloading

#include <iostream>

class Complex {
public:
    double real, imag;

    Complex(double r = 0, double i = 0) : real(r), imag(i) {}

    Complex operator + (const Complex& other) {
        return Complex(real + , imag + );
    }

    void display() {
        std::cout << real << " + " << imag << "i" << std::endl;
    }
};

int main() {
    Complex c1(3.0, 4.0), c2(1.0, 2.0);
    Complex c3 = c1 + c2;
    (); // exports: 4 + 6i

    return 0;
}

Examples of runtime polymorphism

virtual function

#include <iostream>

class Base {
public:
    virtual void show() {
        std::cout << "Base class show function" << std::endl;
    }
};

class Derived : public Base {
public:
    void show() override {
        std::cout << "Derived class show function" << std::endl;
    }
};

int main() {
    Base* basePtr;
    Derived derivedObj;
    basePtr = &derivedObj;

    basePtr->show(); // exports: Derived class show function

    return 0;
}

Interfaces and Abstract Classes

#include <iostream>

class Shape {
public:
    virtual void draw() = 0; // purely virtual function
};

class Circle : public Shape {
public:
    void draw() override {
        std::cout << "Drawing Circle" << std::endl;
    }
};

class Square : public Shape {
public:
    void draw() override {
        std::cout << "Drawing Square" << std::endl;
    }
};

int main() {
    Shape* shape1 = new Circle();
    Shape* shape2 = new Square();

    shape1->draw(); // exports: Drawing Circle
    shape2->draw(); // exports: Drawing Square

    delete shape1;
    delete shape2;

    return 0;
}