Welcome back to CodeYourCraft! Today, we're diving into one of the most powerful features of C++ - Runtime Polymorphism. Let's embark on this exciting journey together!
Runtime Polymorphism, also known as Dynamic Polymorphism, is a feature that allows objects of different types to be treated as objects of a common type. It's all about execution time binding, meaning the specific method to be called is determined at runtime based on the object's actual type.
Runtime Polymorphism provides a high degree of flexibility and reusability in code. It allows us to write general code that can handle objects of different classes, making our programs more adaptable and easier to maintain.
Virtual functions are the foundation of Runtime Polymorphism in C++. A virtual function in a base class can be overridden by derived classes to provide customized behavior.
// Base Class
class Shape {
public:
virtual void draw() { cout << "Drawing a Shape" << endl; }
};
// Derived Class
class Circle : public Shape {
public:
void draw() { cout << "Drawing a Circle" << endl; }
};In the above example, the draw function in the Shape class is declared as virtual, allowing it to be overridden by the Circle class.
When we invoke the draw function on a Circle object, the correct version (the one defined in the Circle class) is called at runtime.
int main() {
Circle circle;
circle.draw(); // Output: "Drawing a Circle"
return 0;
}The process of determining the actual function to be called at runtime is known as dynamic binding or late binding. This is what makes Runtime Polymorphism so versatile and powerful!
Let's create a polymorphic base class that can handle different types of shapes.
// Base Class
class Shape {
public:
virtual void draw() = 0;
virtual double area() = 0;
};
// Derived Classes
class Circle : public Shape {
public:
Circle(double radius) : _radius(radius) {}
void draw() override { cout << "Drawing a Circle" << endl; }
double area() override { return 3.14 * _radius * _radius; }
private:
double _radius;
};
class Rectangle : public Shape {
public:
Rectangle(double width, double height) : _width(width), _height(height) {}
void draw() override { cout << "Drawing a Rectangle" << endl; }
double area() override { return _width * _height; }
private:
double _width, _height;
};
int main() {
Shape* shapes[2];
shapes[0] = new Circle(5);
shapes[1] = new Rectangle(4, 6);
for (int i = 0; i < 2; ++i) {
shapes[i]->draw();
cout << "Area: " << shapes[i]->area() << endl;
}
return 0;
}In this example, we've created a polymorphic base class Shape with draw and area as virtual functions. We then created two derived classes, Circle and Rectangle, each overriding the draw and area functions to provide their own implementations.
In the main function, we create an array of pointers to Shape objects, each pointing to a Circle or Rectangle object. When we call draw and area on these objects, the correct version is called based on the actual type of the object.
Which of the following keywords is used to declare a virtual function in C++?
That's all for today! We hope you've enjoyed learning about Runtime Polymorphism in C++. With this newfound knowledge, you're one step closer to mastering C++!
Stay tuned for more exciting lessons at CodeYourCraft! š