What Is Polymorphism - (Chat GPT) WhatsApp
What Is Polymorphism - (Chat GPT) WhatsApp
*Types of Polymorphism*
Compile-time polymorphism is resolved during the *compilation* phase. The most common
types of compile-time polymorphism are:
- *Method Overloading*
- *Operator Overloading*
*Method Overloading*:
- *Method overloading* occurs when multiple methods in the same class have the same name
but differ in the number or types of parameters.
[25/12, 11:00 pm] ChatGPT: - The correct method is called based on the number or type of
arguments passed.
class Print {
public:
void display(int i) {
cout << "Integer: " << i << endl;
}
void display(double d) {
cout << "Double: " << d << endl;
}
void display(string s) {
cout << "String: " << s << endl;
}
};
int main() {
Print p;
p.display(5); // Calls display(int)
p.display(3.14); // Calls display(double)
p.display("Hello"); // Calls display(string)
return 0;
}
```
In this example, the *display()* method is overloaded to handle three types of inputs: integer,
double, and string.
*Operator Overloading*:
- *Operator overloading* allows the definition of custom behaviors for operators like `+`, `-`, `=`,
`*`, etc., when used with objects.
- This is specific to C++ and other languages that support operator overloading.
class Complex {
public:
int real, imag;
int main() {
[25/12, 11:00 pm] ChatGPT: Complex c1, c2, c3;
c1.real = 3; c1.imag = 4;
c2.real = 1; c2.imag = 2;
Run-time polymorphism is resolved during the *execution* phase. It allows objects of different
classes to be treated as objects of a common superclass, with the appropriate method being
called based on the object type.
The key mechanism for achieving run-time polymorphism is *method overriding*, which occurs
in *inheritance* hierarchies.
*Method Overriding*:
- *Method overriding* occurs when a method in a base class is redefined (overridden) in a
derived class with the same signature (same name and parameters). The method in the derived
class provides a new implementation of the method.
- In run-time polymorphism, the method that gets called is determined at runtime based on the
actual object type, not the reference type.
// Base Class
class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound" << endl;
}
};
[25/12, 11:00 pm] ChatGPT: // Derived Class
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;
}
};
Dog dog;
Cat cat;
return 0;
}
```
In this example, the *sound()* method is overridden in both the *Dog* and *Cat* classes. The
*animalPtr* (of type `Animal*`) is used to refer to objects of different classes, but at *runtime*,
the appropriate method (`sound()`) is called depending on the actual object (`dog` or `cat`).
- *Virtual Functions*: In C++, for run-time polymorphism to work, the base class function should
be declared as `virtual`. This tells the compiler to use dynamic binding (late binding), ensuring
that the correct method is called based on the object type.
---
---
*Advantages of Polymorphism*
1. *Code Reusability*: Polymorphism allows code to be reused across different classes, which
reduces redundancy.
2. *Flexibility*: Code that works with base class references can seamlessly interact with objects
of derived classes, making the code more flexible and extensible.
3. *Maintainability*: It simplifies code maintenance and extension since new derived classes can
be added without modifying the code that uses polymorphism.
4. *Improved Code Organization*: It allows a more organized code structure, as behavior can be
shared among multiple classes through inheritance and overridden methods.
---
[25/12, 11:00 pm] ChatGPT: *Real-World Examples of Polymorphism*
---
*Conclusion*