Practical work: Modern C++ (11 / 14)

code

Below you’ll find a list of problems I’ve made for testing the modern C++ knowledge level.

All the answers are in my other posts, feel free to check them here or in the following link.

1. Create a Base class with a pure virtual function and a virtual destructor
2. Create an enum class with a few values (base : int)

3. Create a class that inherits from Base
– Add a bool and an integer (Use in-class member initializers)
– Add delete (copy ctor / assignment)
– Add default (move ctor /assignment)
– Add override on virtual functions / Add final on the pure virtual function
– Use inheriting constructors
– Use delegating constructors
– Add explicit conversion operators (based on the bool status).

4. Create an alias for hash map with key-value (key = enum class, value = shared_ptr to derived class).

5. Create a function called IterateMap that Iterates through the map (use ranged-based for loop) and display the memory address of all the items that has the bool status to true. (use a lambda)

5. Create a function called CountMap that calls and returns the result of the count_if algo (count how many items in the map have the integer value bigger than some chosen value.)
(HINT! Count_if unary pred will receive as param the following “const std::pair<keyClass, shared_ptr<Derived>>”)

6. Create a template function Called DisplayParams that receive variadic number of parameters.
– Use static assert and type traits (Use the “is_arithmetic” trait.) and display the parameters to the console (cout).

7.
main():
– Create an instance from above-written map alias -> Use initializer list to insert a few values into the map.
– Call IterateMap
– Call CountMap, Display the result to cout
– Get the first value from the map, Call DisplayParams with the data from the class (bool, int)
– Use chrono (high_resolution_clock) to calculate how long it takes for the program to run. Compare the result with a given value (using the user defined literals from chrono_literals)
and display on the screen whether the time was lower (if current_time < 1h30m) cout << “Yay”;
(HINT! use chrono::duration_cast<chrono_minutes> or similar)

Don’t forget to use auto whenever you see fit (ONLY IF the intent and the type is clear from the code)
Try to use constexpr when possible, and noexcept for functions that you’re sure they don’t throw exceptions.

Leave a comment

Your email address will not be published.