cpp version of answer to cracking code interview