Component-based programming is a method that relies on composition :)
Composition == the act of building larger things out of smaller parts.
Component == a small part.
In OOP, the term "component" is informal language. A "class" (a formal term) could informally be called a component. When a class contains another class as a member, you've composed the smaller class/component into the larger class/component.
In other paradigms, a "component" may be more strictly defined.
I am wondering if there is a composition based version of this same demo?
The same, but delete the Shape class and don't use inheritance :lol:
Also note that these kinds of examples are often ok for teaching you how to use the tool -- the syntax of inheritance in a particular language -- but are often terrible for teaching the why. OO inheritance must obey the Liskov Substitution Principle, meaning that any algorithm that works on a Shape, must also work perfectly fine on a Triangle or a Square. May of these beginner level teachings on inheritance do a terrible job of teaching, and actually present examples that violate the LSP...
So for the inheritance version of the Shapes class to even make sense to begin with, you need to first have an algorithm that works exactly the same way on Triangles and Squares. If you design your classes before that algorithm is known, then you're doing it wrong.