Polymorphism in C++
Polymorphism is one of the four fundamental concepts of Object-Oriented Programming (OOP) in C++. It allows objects of different types to be treated as objects of a common base class. Polymorphism enables functions to use objects of different types through a single interface.
1. What is Polymorphism?​
Polymorphism means "many shapes" in Greek. In C++, polymorphism allows you to redefine the way functions behave for different types. It can be classified into two main types:
- Compile-time Polymorphism (Static Binding): Achieved using function overloading and operator overloading.
- Runtime Polymorphism (Dynamic Binding): Achieved using inheritance and virtual functions.
2. Types of Polymorphism in C++​
a. Compile-Time Polymorphism (Static Polymorphism)​
Compile-time polymorphism occurs when the function to be invoked is determined at compile time. It is achieved using:
- Function Overloading
- Operator Overloading
i. Function Overloading​
Function overloading allows multiple functions with the same name but different parameter types or numbers to coexist.
Example:​
#include <iostream>
using namespace std;
class Print {
public:
void display(int i) {
cout << "Displaying int: " << i << endl;
}
void display(double d) {
cout << "Displaying double: " << d << endl;
}
void display(string s) {
cout << "Displaying string: " << s << endl;
}
};
int main() {
Print obj;
obj.display(5);
obj.display(6.7);
obj.display("Hello");
return 0;
}
Output:​
Displaying int: 5
Displaying double: 6.7
Displaying string: Hello
ii. Operator Overloading​
Operator overloading allows you to redefine the way operators work for user-defined types (such as classes).
Example:​
#include <iostream>
using namespace std;
class Complex {
private:
float real, imag;
public:
Complex() : real(0), imag(0) {}
Complex operator + (const Complex &obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
void display() {
cout << "Real: " << real << ", Imaginary: " << imag << endl;
}
};
int main() {
Complex c1, c2, result;
result = c1 + c2;
result.display();
return 0;
}
Output:​
Real: 0, Imaginary: 0
3. Runtime Polymorphism (Dynamic Polymorphism)​
Runtime polymorphism occurs when the function call is resolved at runtime. It is achieved using inheritance and virtual functions.
a. Virtual Functions​
A virtual function is a member function that is declared within the base class and redefined by a derived class. It is used to achieve runtime polymorphism.
Syntax:​
class Base {
public:
virtual void function_name() {
// Base class code
}
};
Example of Runtime Polymorphism:​
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "This is an animal sound." << endl;
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "The dog barks." << endl;
}
};
class Cat : public Animal {
public:
void sound() override {
cout << "The cat meows." << endl;
}
};
int main() {
Animal* animalPtr;
Dog dog;
Cat cat;
animalPtr = &dog;
animalPtr->sound(); // Calls Dog's sound()
animalPtr = &cat;
animalPtr->sound(); // Calls Cat's sound()
return 0;
}
Output:​
The dog barks.
The cat meows.
4. Pure Virtual Functions and Abstract Classes​
A pure virtual function is a virtual function with no definition in the base class, and a class containing a pure virtual function is called an abstract class. You cannot instantiate an abstract class.
Syntax:​
class AbstractBase {
public:
virtual void show() = 0; // Pure virtual function
};
Example of Abstract Class:​
#include <iostream>
using namespace std;
class Shape {
public:
virtual void area() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void area() {
cout << "Area of Circle" << endl;
}
};
int main() {
Circle c;
c.area(); // Calls Circle's area function
return 0;
}
Output:​
Area of Circle
5. Advantages of Polymorphism​
- Code Reusability: Allows functions to operate on different types.
- Flexibility: Easier to change or extend the system with new types.
- Maintainability: Polymorphism simplifies code structure and reduces complexity.
6. Final Thoughts​
Polymorphism in C++ provides a flexible and powerful mechanism for handling multiple types of objects and making your code more scalable and maintainable. It's a crucial concept in OOP and is used extensively in software development.
Happy coding!