课程: Complete Guide to C++ Programming Foundations

免费学习该课程!

今天就开通帐号,24,100 门业界名师课程任您挑!

Solution: Create a person class

Solution: Create a person class

(upbeat music) - [Instructor] For this challenge, your task was to create a class to simulate three attributes of a person in a simulation game, along with three activities that modify these attributes. In the test code area, we have the variable initializations to reproduce example number one in the instructions. Let me show you my solution. Starting at line 14, we have the private members, which are all attributes. Then in line 20, we start with the public members, which for this class are all functions. The first one is the constructor. Notice that its parameters are the initial values for the data members in the class. That's name, energy, happiness, and health. Notice the member initializer list in line 23. This constructor doesn't really require a body as there's nothing left to do. In line 26, we have the eat function, which increases the person's energy. In line 28, we make sure the energy doesn't exceed 100 in the process. Next, starting at lines 32 and 40, the play and sleep…

内容