Polymorphism

Polymorphism derived from Greek word poly (many) and morphe (form) means many forms. In C++, Polymorphism allows different classes to be treated uniformly through a common base class. This concept is divited into two types: complie time (static) polymorphism and runtime (dynamic) polymorphism.

Compile Time Polymorphism (Static Polymorphism)

  • Also known as early binding or method overloading.
  • Occurs at compile time, when the compiler determines which function or method to call based on the number and types of arguments.
  • Typically achieved through function overloading and operator overloading.

Example: Consider a simple calculator that needs to perform addition for various data types (int, double, and float). Instead of creating separate functions for each data type, you can use compile-time polymorphism by overloading the addtion operator.

C++




class Calculator {
public:
    int add(int a, int b) { return a + b; }
 
    double add(double a, double b) { return a + b; }
 
    float add(float a, float b) { return a + b; }
};


In this example, the add fucntion is overloaded to handle different data types, allowing you to use a single function name for addition operations.

Runtime Polymorphism (Dynamic Polymorphism)

  • Also known as late binding.
  • Occurs at runtime, where the specific function to be executed is determined dynamically based on the object’s actual type.
  • Achieved through virtual functions and inheritance.

Example: Imagine a university management system with various staff members like professors and administrative staff. Both have a common method called displayInfo, but the way they display their information is different. You can use runtime polymorphism by creating a base class and derived classses.

C++




class Staff {
public:
    virtual void displayInfo()
    {
        std::cout << "Base class: Staff" << std::endl;
    }
};
 
class Professor : public Staff {
public:
    void displayInfo() override
    {
        std::cout << "Derived class: Professor"
                  << std::endl;
    }
};
 
class AdministrativeStaff : public Staff {
public:
    void displayInfo() override
    {
        std::cout << "Derived class: Administrative Staff"
                  << std::endl;
    }
};


In this example, the Staff class has a virtual function displayInfo. When you call this function on objects on the derived classes, the appropriate version of displayInfo is invoked based on the acutal type of the object.

Object Model | Object Oriented Analysis & Design

Object-Oriented Programming (OOP) is a fundamental paradigm in modern software development that has transformed the way we design, build, and maintain software systems. OOP is centered around the concept of objects, which are self-contained, reusable units that encapsulate both data and the operations that can be performed on that data. This approach mirrors real-world modeling, making it easier to understand, manage, and expand software projects.

Important Topics for the Object Model

  • Objects and Classes
  • Encapsulation and Data Hiding
  • Message Passing
  • Inheritance
  • Polymorphism
  • Generalization and Specialization
  • Association and Aggregation
  • Benefits of Object Model
  • Conclusion

Similar Reads

Objects and Classes

What is a Class?...

Encapsulation and Data Hiding

...

Message Passing

...

Inheritance

Encapsulation and Data Hiding are two fundamental principles of object-oriented programming....

Polymorphism

...

Generalization and Specialization

...

Association and Aggregation

Message Passing is a fundamental conecpt in computer science and programming especially in the context of concurrent and distributed systems. It refers to a method of communication between different processes or objects, where they exchange information by sending and receiving messages. This enables the synchronization and cooordination of activities in a multi-threaded or distributed environment....

Benefits of Object Model

Inheritance is a fundamental concept in object-oriented programming (OOP), and it allows you to create a new class that is based on an existing class. This existing class is referred as the base class or parent class and the new class is called the derived class or child class. Inheritance facilitates code reusability and the creation of a hierarchical structure for classes....

Conclusion

...

Contact Us