Polymorphism in C++ Standard Template Library
Polymorphism is a fundamental concept in object-oriented programming, and it allows objects of different classes to be treated as objects of a common class. In C++, polymorphism is achieved through inheritance and virtual functions.
The Standard Template Library (STL) in C++ provides support for polymorphism through its use of templates. Templates are a powerful feature of C++ that allow classes and functions to work with any data type, not just specific data types. The STL uses templates to define classes such as std::vector
and std::map
that can store and manipulate objects of any type.
Here's an example of polymorphism in the STL:
#include <iostream>
#include <vector>
class Shape {
public:
virtual void print() = 0;
};
class Square : public Shape {
public:
void print() {
std::cout << "I am a square" << std::endl;
}
};
class Circle : public Shape {
public:
void print() {
std::cout << "I am a circle" << std::endl;
}
};
int main() {
std::vector<Shape*> shapes;
shapes.push_back(new Square());
shapes.push_back(new Circle());
for (auto shape : shapes) {
shape->print();
}
return 0;
}
In this example, the class Shape
is defined as a base class with a pure virtual function print()
. The classes Square
and Circle
inherit from the Shape
class and provide their own implementation of the print()
function.
A std::vector
of Shape
pointers is created and two objects of Square
and Circle
classes are added to it. The std::vector
can store objects of different types, including objects of the Square
and Circle
classes, because they are derived from the Shape
class.
In the loop, each object in the vector is accessed and the print()
function is called on it. The correct implementation of the print()
function is called for each object, even though they are stored as pointers to the base class Shape
. This is because of polymorphism and the use of virtual functions in the derived classes.
This example demonstrates how polymorphism can be used in the STL to create generic containers that can store objects of different types and treat them as objects of a common type. The use of polymorphism and templates in the STL provides great flexibility and power to C++ programmers.
polymorphism is a powerful feature in object-oriented programming that allows objects of different classes to be treated as objects of a common class. In C++, polymorphism is achieved through inheritance and virtual functions. The Standard Template Library (STL) in C++ provides support for polymorphism through its use of templates, which allow classes and functions to work with any data type.
For example, by using polymorphism and templates in the STL, a std::vector
can store objects of different types, and the correct implementation of a virtual function can be called for each object, even though they are stored as pointers to the base class.
polymorphism is an important concept in object-oriented programming, and it plays a crucial role in the design and implementation of large-scale software systems. The STL in C++ provides a convenient and powerful way to take advantage of polymorphism, and it is widely used by C++ programmers to write flexible and generic code.