Lecture 4: C++ Fundamentals With Use Cases From Finance: Ivan Zhdankin
Lecture 4: C++ Fundamentals With Use Cases From Finance: Ivan Zhdankin
Ivan Zhdankin
13.06.2021
Class Account
Imagine we have different type of accounts (derived classes) inherited from the Account (base
class)
The reference can be of base class and can actually refer to the derived class instance
The class CurrentAccount inherits from Account has all the capabilities of Account
Any base class function can be called through a base reference to a derived class instance
However if there are same functions implemented in both a base and a derived classes which
function will be executed?:
I If the function in a base class is marked as virtual - the derived class function will be executed
I This is also known as polymorphism - as a function as several forms
I If the function is NOT virtual - the base class function will be executed
Virtual function - is dynamic polymorphism as which function to call is resolved in runtime (some
times this is called run-time polymorphism)
Virtual has impact on the run-time performance
We can NOT call functions of the derived class by the base class reference which target is derived
class instance:
We can NOT create a derived class reference that refers to a base class instance:
Situation with pointers and inheritance is similar to the reference and inheritance
A pointer to a base class can actually point to a derived class instance:
We can NOT have pointer of the derived class pointing to the base class instance:
There are many advantages of using the inheritance and virtual functions as they enable to write
generic code
However we need to be careful as there is a slicing problem
If we copy objects around a slicing problem can occur:
I When copying a derived object into a base object extra member variable fall away
For example, same rules applies when passing to a function by value
If function takes as parameter a bank account and we pass by value a saving account - a copy will
be made and slicing will happen - we will have bank account inside the function, not a saving
account
To avoid the slicing we use pointers and references
For that reason we pass by reference into functions
Often when we have polymorphism we have a base class pointer but we know that in reality
points to a derived class instance
For example at some point of the program you know that the pointer points to the business account
class which is derived from the bank account, however the pointer is of bank account class
We would like the base class pointer to be a derived class pointer as this would allow us to access
the derived class methods
For these reasons we have casting in C++
Static Casting Dynamic Casting
I Static cast < type > I Dynamic cast < type >
I The static cast happens in compile time I Happens in run time
I Use only if the pointer is of derived class I Because of the run-time checks it is safer
I Does not require virtual functions I Works when there is at least one virtual function
I Results in ”Compiler Error” if the casting fails I Returns ”null” if the casting fails
I Because of there are no any checks the static I Because of the run-time checks the dynamic
casting is faster than the static casting casting is slower but safer then the static casting
Demo: Casting
a 1
b 2
c 3
d 4
So far we have seen different collections in SL including stings, vectors and maps
There are other collections in C++ SL
List: implemented as linked list
I Faster adding the element
I Slower than vector for accessing
However all the collections are similar in use: all of them contain similar methods and iterators
Other examples of collections:
I Queues, dequeue: push and pop from one and both end, FIFO
I Priority queue - has a priority inside a queue
I Set - contains unique element
I Multimap - a map which can have more then one value for one key
In RFQ based protocol dealers produce quotes replying ot the clients requests
The dealers’ pricing logic may be based on the ”willingness” to trade which is measured as
probability to trade
When dealers quote price to the client the following relationship hold between Spread and
Probability To trade:
Binary Search is used to to find a spread and produce the price given probability to trade
Complex numbers:
I #include < complex >
Matrix math:
I #include < numeric >
At the same time for each operator will be more readable with lambda function
Lambdas do not just do operations, they can return values like normal functions:
I The compiler can specify the return type of Lambda
I Normally the developer should specify it as with normal functions
I However the syntax for specifying the return type is different
Demo: Lambda
Any block of code that might throw an exception should be wrapped by try block
Catch blocks should follow the try block
Try and catch block should be as close to the problem as possible
Do not use try and catch when you do not expect potential issues
Catch more specific exceptions first
Catch exceptions by reference