Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects to be treated as instances of their parent class rather than their actual class. In C++, polymorphism comes in several forms: function overloading, operator overloading, virtual functions, and pure virtual functions. This blog post will provide a detailed overview of each type, demonstrating their importance and use cases in C++ programming.
What is Polymorphism?
Polymorphism means “many forms.” In C++, it allows functions to behave differently based on the object that calls them. There are two main types of polymorphism in C++:
- Compile-time Polymorphism: Achieved through function overloading and operator overloading.
- Run-time Polymorphism: Achieved through virtual functions and pure virtual functions.
1. Function Overloading
Function overloading allows you to have multiple functions with the same name but different parameters. The compiler determines which function to call based on the arguments passed.
Syntax:
void functionName(int a);
void functionName(double a);
void functionName(int a, int b);
Example:
#include <iostream>
using namespace std;
class Print {
public:
void display(int i) {
cout << "Integer: " << i << endl;
}
void display(double f) {
cout << "Float: " << f << endl;
}
void display(string s) {
cout << "String: " << s << endl;
}
};
int main() {
Print obj;
obj.display(5); // Output: Integer: 5
obj.display(5.5); // Output: Float: 5.5
obj.display("Hello"); // Output: String: Hello
return 0;
}
In this example, the display function is overloaded to handle different types of parameters.
2. Operator Overloading
Operator overloading allows you to redefine the way operators work for user-defined types. It helps in making the code more intuitive and readable.
Syntax:
class ClassName {
public:
returnType operator op(const ClassName& obj);
};
Example:
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
Complex() : real(0), imag(0) {}
Complex(float r, float i) : real(r), imag(i) {}
Complex operator + (const Complex& c) {
return Complex(real + c.real, imag + c.imag);
}
void display() {
cout << "Real: " << real << ", Imaginary: " << imag << endl;
}
};
int main() {
Complex c1(3.3, 4.4);
Complex c2(1.1, 2.2);
Complex c3 = c1 + c2; // Using the overloaded + operator
c3.display(); // Output: Real: 4.4, Imaginary: 6.6
return 0;
}
In this example, the + operator is overloaded to add two Complex objects.
3. Virtual Functions
Virtual functions allow derived classes to override methods of base classes. This feature supports run-time polymorphism, enabling dynamic binding and late binding.
Syntax:
class Base {
public:
virtual void functionName();
};
Example:
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() {
cout << "Display from Base" << endl;
}
};
class Derived : public Base {
public:
void display() override {
cout << "Display from Derived" << endl;
}
};
int main() {
Base* b;
Derived d;
b = &d;
b->display(); // Output: Display from Derived
return 0;
}
In this example, the display function in the derived class overrides the base class’s display function, demonstrating run-time polymorphism.
4. Pure Virtual Functions
Pure virtual functions are declared in the base class and must be overridden in derived classes. They make a class abstract, meaning you cannot instantiate it directly.
Syntax:
class Base {
public:
virtual void functionName() = 0;
};
Example:
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing Circle" << endl;
}
};
class Square : public Shape {
public:
void draw() override {
cout << "Drawing Square" << endl;
}
};
int main() {
Shape* shape;
Circle circle;
Square square;
shape = &circle;
shape->draw(); // Output: Drawing Circle
shape = □
shape->draw(); // Output: Drawing Square
return 0;
}
In this example, draw is a pure virtual function, making Shape an abstract class. Both Circle and Square must override the draw function.
Summary
Polymorphism in C++ allows for more flexible and maintainable code. By understanding and utilizing function overloading, operator overloading, virtual functions, and pure virtual functions, you can create more dynamic and versatile programs. Polymorphism not only enhances code reusability but also promotes better design practices in object-oriented programming. Each type of polymorphism serves a unique purpose, and knowing when and how to use them will significantly improve your C++ programming skills.
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.