C++ code to implement it