Hands-On Exercises: Implementing Polymorphism
Here's an example of implementing polymorphism in C++:
#include <iostream>
class Shape {
public:
virtual void draw() {
std::cout << "Drawing a Shape" << std::endl;
}
};
class Circle : public Shape {
public:
void draw() {
std::cout << "Drawing a Circle" << std::endl;
}
};
class Square : public Shape {
public:
void draw() {
std::cout << "Drawing a Square" << std::endl;
}
};
int main() {
Shape *shape1 = new Circle;
Shape *shape2 = new Square;
shape1->draw();
shape2->draw();
return 0;
}
In this example, the class Shape
is the base class and Circle
and Square
are derived classes. The base class Shape
has a virtual function called draw()
which is meant to be overridden by derived classes. The derived classes Circle
and Square
have their own implementations of the draw()
function, which provide a different output.
In the main function, two pointers shape1
and shape2
are declared, pointing to objects of type Circle
and Square
respectively. The draw function of these objects is called through the pointers, and the correct implementation of the function is called based on the type of the object being pointed to.
The output of the program will be:
Drawing a Circle
Drawing a Square
This example demonstrates how polymorphism can be used to dynamically call the appropriate method based on the type of the object, without having to know the exact type at compile-time. This allows for a high level of abstraction and makes the code more flexible and reusable.
To continue with the example, you could add a new derived class and modify the main function to demonstrate polymorphism even further:
#include <iostream>
class Shape {
public:
virtual void draw() {
std::cout << "Drawing a Shape" << std::endl;
}
};
class Circle : public Shape {
public:
void draw() {
std::cout << "Drawing a Circle" << std::endl;
}
};
class Square : public Shape {
public:
void draw() {
std::cout << "Drawing a Square" << std::endl;
}
};
class Triangle : public Shape {
public:
void draw() {
std::cout << "Drawing a Triangle" << std::endl;
}
};
int main() {
Shape *shapes[3];
shapes[0] = new Circle;
shapes[1] = new Square;
shapes[2] = new Triangle;
for (int i = 0; i < 3; i++) {
shapes[i]->draw();
}
return 0;
}
In this modified version of the program, a new derived class Triangle
is added, with its own implementation of the draw
function. The main function now creates an array of Shape
pointers and assigns objects of type Circle
, Square
, and Triangle
to each element in the array. The for
loop iterates over the array and calls the draw
function of each object, demonstrating how polymorphism allows you to call the correct implementation of a method based on the type of the object, even when the objects are stored in an array and the exact type is not known until runtime.
The output of the program will be:
Drawing a Circle
Drawing a Square
Drawing a Triangle
This demonstrates the power of polymorphism in C++ and how it can be used to write flexible and reusable code.