Introduction to C++ Polymorphism
C++ is an object-oriented programming language that allows for the implementation of polymorphism. Polymorphism is a key concept in object-oriented programming, which enables objects of different classes to be treated as objects of a common base class. This feature allows for code reusability, flexibility, and extensibility in C++ programming.
Types of Polymorphism in C++
C++ supports two types of polymorphism: compile-time polymorphism and runtime polymorphism.
1. Compile-Time Polymorphism (Static Polymorphism)
Compile-time polymorphism is achieved through function overloading and operator overloading.
Function Overloading
Function overloading allows multiple functions with the same name but different parameters to coexist in the same scope. The compiler determines which function to call based on the arguments provided. Here’s an example:
#include <iostream>
void print(int num) {
std::cout << "Printing an integer: " << num << std::endl;
}
void print(double num) {
std::cout << "Printing a double: " << num << std::endl;
}
int main() {
print(10);
print(3.14);
return 0;
}
In this example, the print()
function is overloaded to handle both integers and doubles. The appropriate version of the function is called based on the argument type. This is determined at compile-time.
Operator Overloading
Operator overloading allows operators such as +, -, *, etc., to be redefined for user-defined classes. This enables objects of these classes to be used with the same syntax as built-in types. Here’s an example:
#include <iostream>
class Vector {
public:
int x, y;
Vector operator+(const Vector& other) {
Vector result;
result.x = this->x + other.x;
result.y = this->y + other.y;
return result;
}
};
int main() {
Vector v1 {1, 2};
Vector v2 {3, 4};
Vector sum = v1 + v2;
std::cout << "Sum: (" << sum.x << ", " << sum.y << ")" << std::endl;
return 0;
}
In this example, the +
operator is overloaded for the Vector
class to perform vector addition. This allows us to add two Vector
objects using the +
operator.
2. Runtime Polymorphism (Dynamic Polymorphism)
Runtime polymorphism is achieved through function overriding and virtual functions.
Function Overriding
Function overriding allows a derived class to provide its own implementation of a function that is already defined in its base class. The function in the derived class must have the same signature (name and parameters) as the base class function. Here’s an example:
#include <iostream>
class Shape {
public:
virtual void draw() {
std::cout << "Drawing a shape." << std::endl;
}
};
class Circle : public Shape {
public:
void draw() override {
std::cout << "Drawing a circle." << std::endl;
}
};
int main() {
Shape* shape = new Circle();
shape->draw();
delete shape;
return 0;
}
In this example, the Shape
class has a virtual function draw()
. The Circle
class overrides this function to provide its own implementation. When a Circle
object is accessed through a Shape
pointer, the overridden function in the Circle
class is called at runtime.
Virtual Functions
Virtual functions are functions declared in a base class that can be overridden by derived classes. They are declared using the virtual
keyword. Here’s an example:
#include <iostream>
class Animal {
public:
virtual void makeSound() {
std::cout << "The animal makes a sound." << std::endl;
}
};
class Dog : public Animal {
public:
void makeSound() override {
std::cout << "The dog barks." << std::endl;
}
};
int main() {
Animal* animal = new Dog();
animal->makeSound();
delete animal;
return 0;
}
In this example, the Animal
class has a virtual function makeSound()
. The Dog
class overrides this function to provide its own implementation. When a Dog
object is accessed through an Animal
pointer, the overridden function in the Dog
class is called at runtime.
Conclusion
C++ polymorphism allows for code flexibility and reusability by treating objects of different classes as objects of a common base class. By understanding and utilizing compile-time polymorphism (through function overloading and operator overloading) and runtime polymorphism (through function overriding and virtual functions), C++ programmers can create more versatile and extensible code.
Remember, polymorphism is just one of the many powerful features that C++ offers to enhance your programming experience.