Coding - practical 10 Flashcards
What do arrays allow us to do?
Arrays allow us to group together a number of variables in a single statement.
Give an example of an array for 4 note numbers.
std::array notes; (instead of) int note1; int note2; int note3; int note4;
What is the general form of an array?
- Variable type (e.g. std::array)
- Template initialisation ()
- Name of our array (notes;)
Describe template initialisation.
Template initialisation is basically a way to tell the compiler what kind of array we want.
- The triangle brackets ( < > ) are used to tell the compiler what kind of array you want.
- Within this is the type (e.g. int) and size (e.g. 4)
What is reading and writing achieved by?
[ ] the square brackets
What is the size function?
A class method for the std::array class, and returns the number of elements in the array. - array.size()
How do you make a variable const(read only)?
To make a variable const (read only) we use the const keyword in front of the variable type. Once a variable has been set as const, its value cannot be changed.
What are the two main uses of const?
Preventing errors
- Stops you trying to change values that should stay the same
- Can make code more readable
How else can you define constant values?
with the define keyword
- The general form of a define is: #define SYMBOL value
What is an enum?
An enum is shorting for enumeration and
allows a series of constant symbols to be defined in a series, whereby each enums value is +1 of the previous value.
Give an example of an enum.
enum waveTypes { eSine = 0; eSquare, eSaw, eReverseSaw, eTri, eNoise, };