5.3 OBJECT-ORIENTED LANGUAGES
In a traditional, non-object-oriented framework, we have a few options in implementing the price report in Figure 5.1, knowing that the trades in the set we are reporting on are of different types. We can examine the trade type in the loop and call the right pricing function depending on what kind of trade it is that we are handling. Alternatively, we could loop over each kind of trade separately, creating multiple loops, or we could call the same pricing function, which can examine the trade type and take the appropriate action. All of these options are far from elegant.
If we could associate the pricing function with the complex data structure that describes a trade, we will keep the same simple program flow in Figure 5.1, while accomplishing customized pricing for each trade type. This functionality of encapsulating and associating functions with data structures is provided by object-oriented programming (OOP) methodology. In OOP terminology, the functions are methods and structures are classes or objects. A class can be thought of as a data structure with associated functions specific to it and an object is an instantiation of a class. With a {trade} class defined, the declaration Trade t ; creates an object t of the trade class. In this sense, a class is like a type and an object is a variable of the class type.
The object-oriented paradigm originated out of the difficulty in maintaining large and complex programs in the conventional sequential ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access