The Open/Closed Principle (OCP) is one of the five SOLID principles of object-oriented design. It asserts that a class should be designed in such a way that its behavior can be extended without altering its source code. By adhering to this principle, developers can add new functionality to a system without risking the introduction of bugs in existing code.
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
- Imagine a
Shape
class that handles various shapes likeCircle
andSquare
. - If we want to add a new
shape
, we would need to modify theShape
class, which violates the OCP.
#include <iostream>
#include <string>
#include <cmath>
class Shape {
public:
std::string type;
double radius;
double side;
double calculateArea() {
if (type == "circle") {
return M_PI * radius * radius;
} else if (type == "square") {
return side * side;
}
return 0;
}
};
int main() {
Shape circle;
circle.type = "circle";
circle.radius = 5.0;
std::cout << "Area of Circle: " << circle.calculateArea() << std::endl;
Shape square;
square.type = "square";
square.side = 4.0;
std::cout << "Area of Square: " << square.calculateArea() << std::endl;
return 0;
}
- To correct this, we can use
polymorphism
. Create anabstract Shape
class with acalculateArea()
method. - Each specific shape (e.g.,
Circle
,Square
) will extend theShape
class and implement thecalculateArea()
method.
#include <iostream>
#include <cmath>
class Shape {
public:
virtual double calculateArea() const = 0; // Pure virtual function
virtual ~Shape() = default; // Virtual destructor
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() const override {
return M_PI * radius * radius;
}
};
class Square : public Shape {
private:
double side;
public:
Square(double s) : side(s) {}
double calculateArea() const override {
return side * side;
}
};
int main() {
Shape* circle = new Circle(5.0);
std::cout << "Area of Circle: " << circle->calculateArea() << std::endl;
delete circle;
Shape* square = new Square(4.0);
std::cout << "Area of Square: " << square->calculateArea() << std::endl;
delete square;
return 0;
}