Added C++ implementation (exact same code).