课程: Complete Guide to C++ Programming Foundations
免费学习该课程!
今天就开通帐号,24,100 门业界名师课程任您挑!
Solution: Create a person class - C++教程
课程: Complete Guide to C++ Programming Foundations
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…
内容
-
-
-
-
-
-
-
-
-
(已锁定)
Overview of classes and objects5 分钟 13 秒
-
(已锁定)
Data members3 分钟 1 秒
-
(已锁定)
Function members8 分钟 8 秒
-
(已锁定)
Constructors and destructors5 分钟 15 秒
-
(已锁定)
Using classes2 分钟 26 秒
-
(已锁定)
Header and implementation files4 分钟 23 秒
-
(已锁定)
Working with several source files7 分钟 8 秒
-
(已锁定)
Operator overload4 分钟 46 秒
-
(已锁定)
Solution: Create a person class2 分钟 22 秒
-
(已锁定)
-
-
-