diff --git a/CM2005 Object Oriented Programming/Topic 2/2.3.13/a.exe b/CM2005 Object Oriented Programming/Topic 2/2.3.13/a.exe new file mode 100644 index 0000000..15f171e Binary files /dev/null and b/CM2005 Object Oriented Programming/Topic 2/2.3.13/a.exe differ diff --git a/CM2005 Object Oriented Programming/Topic 2/2.3.13/main.cpp b/CM2005 Object Oriented Programming/Topic 2/2.3.13/main.cpp new file mode 100644 index 0000000..647bc30 --- /dev/null +++ b/CM2005 Object Oriented Programming/Topic 2/2.3.13/main.cpp @@ -0,0 +1,99 @@ +#include +#include +#include + +enum class OrderBookType +{ + bid, + ask +}; + +class OrderBookEntry +{ + public: + OrderBookEntry( + double price, + double amount, + std::string timestamp, + std::string product, + OrderBookType orderType); + double price; + double amount; + std::string timestamp; + std::string product; + OrderBookType orderType; +}; + +//constructor declaration. It needs to be placed after the class declaration +OrderBookEntry::OrderBookEntry( + double price, + double amount, + std::string timestamp, + std::string product, + OrderBookType orderType +) +: price{price}, amount{amount}, timestamp{timestamp}, product{product}, orderType{orderType} +{ + +} + +double computeAveragePrice(std::vector& entries) +{ + double avg = 0; + for(unsigned int i=0; i orders; + + orders.push_back( OrderBookEntry{ + 10000, + 0.002, + "2020/03/17 17:01:24.884492", + "BTC/USDT", + OrderBookType::bid}); + + orders.push_back( OrderBookEntry{ + 50000, + 0.002, + "2020/03/17 17:01:24.884492", + "BTC/USDT", + OrderBookType::bid}); + + // Version 1 + std::cout << "Version 1" << std::endl; + for(OrderBookEntry order : orders) //this create a copy of the members of orders and assigns it to 'order' + { + std::cout << "The price is " << order.price << std::endl; + } + + std::cout << "---new line---" << std::endl; + + for(OrderBookEntry &order : orders) //this doesn't crate a copy + { + std::cout << "The price is " << order.price << std::endl; + } + + // Version 2 + std::cout << "Version 2" << std::endl; + for(unsigned int i=0; i