Some “hello world”-style object oriented programs. These will get you started writing object oriented programs in various languages.
Inspired by Rosetta Code
A Partial List of Languages:
c structs (?)
octave objects (?)
List of Programs/Classes
For each language, we try to illustrate a few key behaviors.
Basic object operations:
- Point object to illustrate a basic object with a Cartesian point
- Point object to illustrate comparisons of objects/equality checking
- Point3D object to illustrate iteration
- Widget object to illustrate printing “Hello World” from an object method
- Shape object to illustrate inheritance (Circle, Rectangle, Triange)
- Shape object to illustrate interfaces (getArea, getPerimeter)
- Floorplan object to store Shape array, sum areas of each, to illustrate polymorphism
- How to protect data, while also providing public accessors to get/set
- How to make data truly immutable?
Objects in memory:
- Point3D object to illustrate how objects are dealt with in memory
- If we say point2 = point1, what really happens?
- Illustrating a copy constructor (taking object of type self)
- How to check if two objects are equal?
- How to store pointers or references to objects?
- Recursive composition - hierarchical structure of elements that builds increasingly complex structures out of simple ones; when operations performed on parent, operation is recursively passed down hierarchy.
- Composite pattern - collection of nodes to make up a whole.
- Compositor - has an associated instance of the Composition object; when Compose() called, iterates through each composition element and rearranges structure as needed.
- Strategy pattern - encapsulate multiple algorithms to be used based on changing context.
- Transparent enclosure - layering elements augmenting overall composition beahvior onto the composition; elements are subclasses of singular element itself; augmenting composition by adding state-like elements.
- Decorator pattern - decorators add behavior to an individual object, without affecting behavior of all other objects of the same type. (Decorator is subclass with pointer to parent, parent instance is passed to constructor, decorator forwards all parent methods to parent, overrides any methods that need to be modified). Decorators can stack. Subclassing is a compile-time modification, decorators are a run-time modification.
- Abstract factory pattern - abstracts the object creation process; different concrete implementations are possible.
Other design patterns:
Following the advice of the “Gang of Four” book on design patterns in programming, the authors recommend programming to interfaces, not to implementations, and recommend object composition over class inheritance.
This basically explains why Go does “OOP” the way it does.