this code is passed on the Big c++ 2nd edition text book (chapter 2, section 2.8)