From the course: Complete Guide to C++ Programming Foundations
Unlock this course with a free trial
Join today to access over 25,000 courses taught by industry experts.
Solution: Create a person class - C++ Tutorial
From the course: 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…
Contents
-
-
-
-
-
-
-
-
-
(Locked)
Overview of classes and objects5m 13s
-
(Locked)
Data members3m 1s
-
(Locked)
Function members8m 8s
-
(Locked)
Constructors and destructors5m 15s
-
(Locked)
Using classes2m 26s
-
(Locked)
Header and implementation files4m 23s
-
(Locked)
Working with several source files7m 8s
-
(Locked)
Operator overload4m 46s
-
(Locked)
Solution: Create a person class2m 22s
-
(Locked)
-
-
-