Revision Exercise - 3
Exercise 0 - Variables and Constants
Part 0 - Constants
How are constant variables defined?
What is the difference between a constant variable and a regular variable?
What is the
constexprkeyword? And when do you useconstexpras opposed toconst? (see: https://docs.microsoft.com/en-us/cpp/cpp/constexpr-cpp?view=msvc-160#constexpr-variables)What is the
typedefkeyword, and when is it used? Show an example.
Part 1 - Auto variables
Based on the following statements, state if the statements are true or false.
An
autovariable can be re-assigned to values of any type (i.e. it is typeless)The underlying type of an
autovariable is determined by the compilerAll
autovariables have to be initialised on declaration
Exercise 1 - Arrays
Part 0 - Arrays
Show an example defining an array of any primitive type (
int,float,long, etc) and initialising its elements.Show an example defining a 2D array of any primitive type and initialising its elements.
Within the same example as 2, write a loop that enumerates over every element in the 2D array, and sums its values together.
Part 1
coming soon
Exercise 2 - Functions
Part 0 - Declaring and Defining
What is a function prototype?
Write the function prototype for a function named
getAreathat accepts two float parameters, and returns another float parameter.When can two functions be defined with the same name?
When can two functions be defined with the same name and return type?
When can two functions be defined with the same function prototype?
What is function overloading?
What is a function definition, and how does it differ from a function prototype?
Part 1 - Function Parameters
How do you define a function with default parameter values?
What is a reference function parameter, and how does it differ from a regular function parameter?
If an object is passed by reference, will its copy constructor be invoked?
Part 2 - Arrays & Functions
What is meant by array decay? Show an example of this.
How do we pass an array into a function? Show an example of passing an array into a function that enumerates over the array and prints its contents.
Part 3 - Lambda Functions
What are lambda functions?
What is the syntax for a lambda function?
Show a simple example using a lambda function, making sure to show the use of the 'captures' feature.
Exercise 3 - Pointers
Part 0 - Variables and Pointers
What are pointers?
How can we get the memory address of a variable? Show an example of getting an address of a variable, and storing it into a pointer.
How do we retrieve the value stored at a memory address through a pointer? Show an example of printing a value stored at a certain memory address using a pointer.
What is the size of an
int*,float*anddouble*? Explain your answer.
Part 1 - Dynamic Values
What is the
newkeyword, and what does it return? Show an example of using thenewkeyword to dynamically allocate afloat.Is memory allocated with
newautomatically released? If not, how do we release memory that was allocated with thenewkeyword?How does the syntax for releasing dynamically allocated memory differ for dynamically allocated values, and dynamically allocated arrays?
What is the implication of not releasing memory that was dynamically allocated?
What is the implication of releasing memory twice?
Part 2 - Constant Pointers
Study each snippet below, and explain whether:
The memory address stored by the pointer
num_ptrcan be changedThe data stored at the address of
num_ptrcan be changed
int num = 30;
int* const num_ptr = #
*num_ptr = 20; // OK?
int number_2 = 20;
number_ptr = &number_2; // OK?int num = 30;
const int* num_ptr = #
*num_ptr = 20; // OK?
int num2 = 20;
num_ptr = &num2; // OK?int num = 30;
const int* const num_ptr = #
*num_ptr = 20; // OK?
int num2 = 20;
num_ptr = &num2; // OK?Part 3 - References
(Refer to the textbook, page 205 if you need help on this)
What are reference variables? Show an example of creating one.
How are reference variables similar to reference function parameters?
Last updated
Was this helpful?