C++ for Quantitative Finance: Hands-On - Object Oriented Programming
APLIED COMPUTATIO, QUANT METHODS
RELEVANT FOR QUANTITATIVE FINANCE
Don't get loss, what you have to understand here is what is a class (blueprint) and an object (instance of class). Then in practice (just code) the four pillars: abstraction, encapsulation, inheritance and polymorphism. Once understand it go to the best practice in modern C++, preferred composition respect to inheritance when is possible.
The content is organized as follows (click to be redirected):
- Setup and Configuration
- Language and Syntax (moderm C++, C++20)
- Debuging
- Memory Management
- Object-Oriented Programming (OOP) [YOUR ARE HERE]
- Efective and Functional Programming
- Generic Programming (GP)
- High Performance Computation
- Applications in Computational Finance
- .
- .
- .
- .
- .
- Data Strcutures and Algorithms
- Reference
Each session have being public in a separate session due to the Blogger content volume restriction.
Go to the main publication (click here).
Object-Oriented Programming (OOP) is a programming paradigm that organizes code around objects, which combine data (attributes) and behavior (methods).
OOP models real-world entities as objects.
In OOP are two important concepts: class and object.
| Concept | Description |
|---|---|
| Class | Blueprint |
| Object | Instance of a class |
OOP have four pillars:
The core principles are Encapsulation, Abstraction, Inheritance, and Polymorphism, with encapsulation and composition being the most frequently used in modern C++ code.
- Abstraction: The essential details (expose); reduces complexity.
- Encapsulation: hide internal details and provide controlled access. Protect from unintended modification.
- Inheritance: create new classes from existing ones, code reuse.
- Polymorphism: multiple implementations of one interface for multiple behaviors. You can see a litle more details at the end of the blog.
In practice when we deal with inheritance we will have to use composition.
Moderm C++ prefer composition over inheritance. Essentially:
| Relationship | Use |
|---|---|
| "is-a" | Inheritance |
| "has-a" | Composition |
Composition creates looser coupling and is easier to maintain. You can change the engine implementation without redesigning the entire class hierarchy.
Use inheritance mainly when you truly need polymorphism (virtual functions and runtime behavior selection). For most code, composition leads to simpler, more flexible, and easier-to-test designs.
The main detail is that composition means building a class from other classes by making them member objects.
Modern C++ Best Practice
- Prefer composition over inheritance when possible.
- Use RAII for resource management.
-
Use
overridefor virtual functions. -
Keep data members
private. - Design small, focused classes.
Class are use to be save in .h files.
a practical example
Constructor
Destructor (~)
Structue (struct)
