# Chapter 3 - Worked Examples

### Question 1

Based on the code block below, predict the values of the variables after each line below has been executed. State, for each line, which of the following is occurring:

* Declaration
* Initialisation&#x20;
* Assignment

```cpp
int a; 

int b = 3;

int c;
c = 5; 

int d = 0;
d = 1;
```

### Question 2

What are the 2 different casing conventions we have covered? Which convention should be used in each of the scenarios below?

1. Function names
2. Variable names&#x20;

### Question 3

For each of the scenarios below, what would be the appropriate data type to use?&#x20;

(There isn't strictly *one* correct answer, so long as you justify your choice)

*e.g. The age of a person: `int` (or `unsigned short`)*

1. The price of a grocery store item&#x20;
2. The global population of people
3. The first letter of a first name&#x20;
4. The circumference of a circle&#x20;

### Question 4

What is the syntax for list initialisation, and what problems does it solve?&#x20;

Based on the following code example, determine for each initialisation if there would be any errors:

```cpp
double x = 0;
int y = 0;
    
// Determine if each of the below lines are

int x2 = x;

char c2 = y;

int x3 {x};

char c3 {y};

char c4 {24};

char c5 {264};

int x4 {2.0};
```

{% hint style="info" %}
**List initialisation** prevents **narrowing**:

* An integer cannot be converted to another integer that cannot hold its value. For example, char to int is allowed, but not int to char.&#x20;
* A floating-point value cannot be converted to another floating-point type that cannot hold its value. For example, float to double is allowed, but not double to float.&#x20;
* A floating-point value cannot be converted to an integer type.&#x20;
* An integer value cannot be converted to a floating-point type.
  {% endhint %}

### Question 5

Based on the following statements, state if the statements are **true** or **false**.&#x20;

* An `auto` variable can be re-assigned to values of any type (i.e. it is typeless)
* The underlying type of an `auto` variable is determined by the compiler&#x20;
* All `auto` variables have to be initialised on declaration&#x20;

### Question 6

What does the keyword `typedef` do, and when might a programmer decide to use it?&#x20;

You are creating a program to calculate the total cost of a shopping list - which data type might you decide to use to represent the cost of each item? Use the `typdef` keyword to give this type an alias called `CURRENCY_TYPE`

### Question 7

* What are **literal constants**? Show an example where a literal constant is used.&#x20;
* How are constant variables defined?&#x20;
* What is the `constexpr` keyword?&#x20;
* What are the benefits to using constants?&#x20;

### Question 8

What are enumerations (enums), and when might a programmer decide to use it?&#x20;

In each of the code blocks below, predict if the program code is valid (compiles without errors), and its output:

```cpp
enum Flowers 
{
    Daisy,
    Lily,
    Sunflower,
    Rose,
    Orchid
}

int main() {
    std::cout << Daisy << std::endl;
    std::cout << Lily << std::endl;
    std::cout << Sunflower << std::endl;
    std::cout << Rose << std::endl;
    std::cout << Orchid << std::endl;
}
```

```cpp
enum Flowers 
{
    Daisy = 10,
    Lily,
    Sunflower,
    Rose,
    Orchid
}

int main() {
    std::cout << Daisy << std::endl;
    std::cout << Lily << std::endl;
    std::cout << Sunflower << std::endl;
    std::cout << Rose << std::endl;
    std::cout << Orchid << std::endl;
}
```

```cpp
enum Flowers 
{
    Daisy = 10,
    Lily = 20,
    Sunflower = 0,
    Rose = 30,
    Orchid = 5,
}

int main() {
    std::cout << Daisy << std::endl;
    std::cout << Lily << std::endl;
    std::cout << Sunflower << std::endl;
    std::cout << Rose << std::endl;
    std::cout << Orchid << std::endl;
}
```

### Question 9&#x20;

You run a grocery store that only opens during weekdays. Create an enum called `Day`, with 7 constants corresponding each day of the week. Then, create a function with the following signature:

```cpp
bool storeIsOpen(Day day);
```

that takes a variable `day` corresponding to one of the values you have defined in `Day`, and returns true for all weekdays (Monday to Friday), and false for all weekends (Saturday and Sunday).&#x20;

You should use a switch-case statement for the function body.&#x20;
