What does line 3 do? stats() {} |
That is the class constructor. Until you initialize your member variables, they will contain garbage. It's in the body of the constructor that you initialize members.
As you can see, I left the body of the constructor blank - I will explain further down.
You didn't include the double array in the class, did I do that part right? Were you just adding to my class? |
The double array is still there. It's on line 22. Your array was named 'Data', but I chose to name mine 'values'.
Unsure of what line 22 and 23 do. |
Let's take a look at line 22:
double values[max_values] = {};
This line declares an array named 'values' with
max_values
number of elements. We know that
max_values
is an integer with a value of 30 by looking at line 21. Therefore, our double array ('values') has 30 elements (doubles).
The
= {};
at the end is taking advantage of a feature that was introduced in C++11, which allows me to initialize non-const member variables (in this case an array) on the line on which it is declared. It used to be that you could only initialize const static integral member variables on the same line on which they are declared, and all other members (such as arrays) had to be initialized in the constructor. If you leave the body of this initialization empty, the default constructor will be invoked - in this case for all 30 doubles, which initializes them to zero.
In summary, line 22 declares an array of doubles named 'values'. It has 30 elements and each one of them is initialized to zero.
Let's take a look at line 23:
int current_value = 0;
I just edited my other post, realizing the type should have been
int
and not
double
.
This is a member variable integer named 'current_value'. I've used the same feature I just described to initialize it to zero on the same line on which it was declared. The instructions you've provided don't explicitly necessitate this variable, but it's heavily hinted that you'll have to implement some sort of counter to keep track of which index is considered to be the last one of your array. That's what this variable does. It's simply meant to be used as a counter in your
storeValue
function.
never used static before but isn't it the same as const? |
const
and
static
are entirely different keywords used for different things, but they're commonly seen together in classes.
const
just means that this variable cannot be modified. It's value can be read, but it cannot change.
static
means that this variable will continue to exist past the current scope. In other words, it's lifetime is not bound by the scope in which it is declared. When your class needs a constant (such as the maximum number of elements in an array), the constant is declared as
const static
.
const
because it cannot change, and
static
because this constant is universally shared between all instances of this class (it's more of a property of the actual class, and not of any instances derived from it).
I hope that makes sense. Let me know if I should clarify some more.