# Revision Exercise - 2

## Exercise 1&#x20;

Given an initialisation of the following structure type, describe what happens with each initialisation. Explain any problems with these initialisations.&#x20;

```cpp
struct Date {
    int month;
    int day;
    int year;
};
```

1. `Date date = {12, 21};`
2. `Date date = {12, 21, 20, 22};`
3. `Date date = {12, 21, 22};`

## Exercise 2&#x20;

Define a structure type to contain information about a given person. The information that this structure should contains are:&#x20;

* The annual salary for the person&#x20;
* The accrued vacation for the person (represented as a whole number of days)
* Status, which is either hourly or salaried, represented by two `char` values 'H' and 'S' respectively.&#x20;

This structure type is to be called `EmployeeRecord`&#x20;

## Exercise 3

### Part 1&#x20;

Given the following class **declaration**:

```cpp
class DayOfYear {
    int day;
    int month;
public:  
    void input();
    void output();
};
```

Implement the `DayOfYear::input` and `DayOfYear::output` functions such that the following program results in the following output:

```cpp
int main() {
    DayOfYear dayOfYear;
    dayOfYear.input();
    dayOfYear.output();
}
```

```
Enter the day of the month: 13
Enter the month as a number: 5
day=13, month=5
```

Define the member functions separately from the class, using the scope resolution operator (`::`).

### Part 2&#x20;

Create a private member function `DateOfYear::check_date` that will be called by `DateOfYear::input` to ensure that the dates being input are valid:&#x20;

* The day of the month cannot be less than 1, and cannot exceed 31.&#x20;
* The month of the year cannot be less than 1, or greater than 12.&#x20;

### Part 3&#x20;

Create two constructors for the `DayOfYear` class. The first should take two `int` parameters corresponding to the day and month respectively. The second should have no parameters which constructs a date represented as January 1.&#x20;

Create two `DayOfYear` variables that use each of these constructors.&#x20;

### Part 4

Suppose we now want to be able to compare two instances of `DayOfYear`s, we can do so by overloading the `==` operator.&#x20;

Create this overload function, that returns `true` if both the `day` and `month` member variables of a given `DayOfYear` is equal to another.&#x20;

```cpp
DayOfMonth first = DayOfMonth(3, 6);
DayOfMonth second = DayOfMonth(3, 6);

if (first == second) {
    std::cout << "The dates are equal!" << std::endl;
}
```

Implement the same for the operator `<`, which tests if a date comes before another.&#x20;

## Exercise 4&#x20;

Consider the following class definition:

```cpp
class Automobile {
private:
    double price;
    double profit;
    double get_profit();
    
public:
    void set_price(double new_price);
    void set_profit(double new_profit);
    double get_price();
};
```

Suppose the program has created two instances of `Automobile` and has set the values of all member variables to some value:

`Automobile hyundai, jaguar;`

Which of the following statements are allowed to be called from `main()`?

1. `hyundai.price = 1999.99`
2. `jaguar.set_price(30000.10)`
3. `double a_price = jaguar.get_price();`
4. `double a_profit = jaguar.get_profit();`
5. `double b_profit = hyundai.get_profit();`

## Exercise 5

Consider the following class definition:

```cpp
class Foo {
public:
    Foo(int x, char y);
    Foo(); 
    void bar();
private:
    int x;
    char y;
};
```

Assuming that all the functions declared have been later defined, which of the following are legal?&#x20;

1. `Foo foo_one(42, 'A')`
2. `Foo foo_two;`
3. `Foo foo_three();`
4. `foo_one = Foo(99, 'B');`
5. `foo_one = Foo();`
6. `foo_one = Foo;`

## Exercise 6&#x20;

1. Define a simple class `Foo` that inherits from a class `Bar`. (Use `public` inheritance)
2. If `Bar` were to have any `private` member variables or functions, would these be accessible in `Foo`? If not, how can we change it so that it can be accessible within `Foo`?

## Exercise 7&#x20;

Given the following class definitions:&#x20;

```cpp
class Fish {
public:
    void greet() {
        std::cout << "I am a Fish" << std::endl;
    }
};

class Tuna {
public:
    void greet() {
        std::cout << "I am a Tuna!" << std::endl;
    }
};
```

Predict the output of the following program:&#x20;

```cpp
int main() {
    Fish fish;
    Tuna tuna;
    
    fish.greet();
    tuna.greet();
    
    tuna.Fish::greet();
    fish.Fish::greet();
}
```

What would be the output if we removed the `Tuna::greet` function?&#x20;
