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):

  1. Setup and Configuration
  2. Language and Syntax (moderm C++, C++20)
  3. Debuging 
  4. Memory Management 
  5. Object-Oriented Programming (OOP) [YOUR ARE HERE]
  6. Efective and Functional Programming 
  7. Generic Programming (GP)
  8. High Performance Computation 
  9. Applications in Computational Finance
    1. .
    2. .
    3. .
    4. .
  10. Data Strcutures and Algorithms
  11. 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.

ConceptDescription
ClassBlueprint
ObjectInstance of a class

 

// Object Oriented Programming
 
// class Car {} -> class 
// car1, car2 -> objects

class Car {
public:
    std::string brand;
   
    void Start() {
        std::cout << "Engine started\n";
    }
};

int main() {
    Car car;
    car.brand = "Toyota";
    car.Start();
}

 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.

  1. Abstraction: The essential details (expose); reduces complexity.
  2. Encapsulation: hide internal details and provide controlled access. Protect from unintended modification.
  3. Inheritance: create new classes from existing ones, code reuse.
  4. Polymorphism: multiple implementations of one interface for multiple behaviors. You can see a litle more details at the end of the blog.


/* ////////////////////////////////////////////////// */
/*  PRINCIPLES OF OOP */
/* Abstraction */

class printer {
    public:
        void print() {
            //
        }
};

/* --------------------------------------------------- */
/* Encapsulation*/

class BankAccount {
    private:
        double balance;

    public:
        void Deposit(double amount) {
            balance += amount;
        }

        double GetBalance() const{
            return balance;
        }
};

/* --------------------------------------------------- */
/* Inheritance */
#include <iostream>

class Animal {
    public:
        void Eat() {
            std::cout << "Eating\n";
        }

        virtual void Speak() {
            std::cout << "Animal sound\n";
        }
};

class Dog: public Animal {
    public:
        void Bark() {
            std::cout << "Woof!\n";
        }

};

Dog dog;
dog.Eat();
dog.Speak();
dog.Bark();

/* --------------------------------------------------- */
/* Polymorphis */

class Dog: public Animal {
    public:
        void Speak() override {
            std::cout << "Woof!\n";
        }
};

class Cat: public Animal {
    public:
        void Speak() override {
            std::cout << "Meow!\n";
        }
};

Animal *a = new Dog();
a->Speak(); //woof:

/* --------------------------------------------------- */
/* --------------------------------------------------- */

 

 

In practice when we deal with inheritance we will have to use composition.

Moderm C++ prefer composition over inheritance. Essentially:

RelationshipUse
"is-a"Inheritance
"has-a"Composition

 

// Composition vs Inheritance

// Inheritance ("is-a")
class Dog : public Animal {
};
 
// Composition ("has-a")
class Car {
    Engine engine; // A Car has an Engine.
};
 

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.

 

/* composition*/

 class Engine {
        public:
            void Start() {
                std::cout << "Engine started\n";
            }
 };

 class Car {
    private:
        Engine engine; //car has an engine [Composition]

    public:
        void Start() {
            engine.Start();
        }
 };

 Car car;
 car.Start();

 

Modern C++ Best Practice

  • Prefer composition over inheritance when possible.
  • Use RAII for resource management.
  • Use override for 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)

 

 

 

 

 

 

 

Entradas populares

Lo mas consultado

Entradas populares