Lecture 2: C++ Fundamentals With Use Cases From Finance: Introduction To OOP in C++
Lecture 2: C++ Fundamentals With Use Cases From Finance: Introduction To OOP in C++
Lecture 2: C++ Fundamentals With Use Cases From Finance: Introduction To OOP in C++
Ivan Zhdankin
06.06.2021
Member Member
Constructor
Variables functions
Setting initial
values of a Student ID Study
Student
Class DoAssignment
Last Name
Current State
Cuemacro Lecture 2: C++ fundamentals with use cases from finance 5 / 35
Class and design
An object is an instance of a class
Function that are inside a class are called member functions
The functions which are not part of a class (discussed so far) are called free or non-member
functions
When we write a class we tell to the compiler what any objects of the class have or can do
I Example of data: a customer has a name, address, email, phone number
I Example of member functions: we can withdraw or debit the account, we can show the balances and
transactions
Keeping the data and what we can do with it together inside a class makes the code easier to
change and use
Let us design a class of Bank account
Account:
I Contains account’s ID number
I Contains Current balance
I Contains list of transaction objects
I Can withdraw and debit some amount of cash
I Can report the balances and the latest transactions
Transaction:
I Holds the date and time of the transaction
I Holds amount and transaction type
I Can report the amount and transaction type
Member function Deposit:
I Create a transaction object
I Add the object to the vector of a transactions
I Update the balances
So far we were using only one file for the source code with extension .cpp
Imagine the big organization with code that has several thousands lines
I Requires compiling it all when making small changes
I Difficult to coordinate the work of the developers
I Difficult to find anything in one single file
In practise the code organised in multiple files to resolve those issues
In case of multiple files one has to compile each of the files in to object file and then link them all
using the linker
If we use the other files in our code we need to explicitly tell the compiler about it
In previous implementation we had to declare the functions and classes before we can use them
Consider the project that has tens of functions and classes, this may become frustrating
We can put all of the declaration in a separate files and then include the file into the code we would
like to compile
The file is called Header file and has extension .h
The directive to include the file would ne: #include
Everything that starts with # will be included into the code by the preprocessor and after that the
whole file will be compiled
As a result your code would look nicer, be more maintainable and easier to understand
Typical approach:
I Have one header file per class to explain and declare what is in the class
I Have one implementation file .cpp per class that implements all the functions of a class
Any code that uses the class should include the header file
We should also include the header file in .cpp file that implements the class
We will have a look at demo ”Classes”
In one file (header file .h) we declare the functions and in another one (implementation file .cpp)
we implement the functions of classes
Some functions in a class are obvious
Often the function work with private variables
For such functions it makes sense to implement them in a header file
These functions are called inline
The advantages of using the inline functions is that it can speed up your application
For example we can make current balance function in Account class to be inline
Any member of a class can have it is own access specifier that defines how the member can be
accessed:
Account a1;
We are indicating with colon that we are inheriting from something else:
Inside the brackets we declare only what we are adding or overriding from the Base class
If we override we provide a special implementation of something that is in a Base class
In general the implementation of Derived classes is of no difference as compared to Base classes
However the constructors are different: we need to pass some parameters to the base class for
initialization
Demo: Inheritance
So far we have seen different functions: free functions, constructors, member functions
The following function takes the parameter by values
It creates its own copy of the variable to work with it inside the function
If something is changed inside the function the variable itself is not changed in this case
If is possible to take a parameter by reference
If we have a line of the parameter that changes it the function also changes the variable
Demo: Free function
We declare the member function in a class, we can also declare it in a header file
If we implement the member function in cpp we use the full name:
Account :: GetName ()
The relationship between SpotFXrate and ForwardFXrate at the start of FX forward is:
That is the price of FX forward at time t is given by [John C. Hull ”Options, Futures and Other
Derivatives” for reference]:
The Call Option can be defined by the following attributes that can be taken into constructor:
I Identifier
I Nominal (N)
I SpotPrice (S)
I interestRate (r)
I dividendRate (d)
I impliedVol (σ)
I Strike (K )
I TTM (t)
Call Option Valuation is implemented in ValueInstrument method
The option price is derived from Black-Scholes model as follows: