# Weekly notes 3

This week we will discuss some additional material on classes in C++. This material comes from chapter 10, but we will also quickly look at inheritance from chapter 12. Inheritance works in a similar way in Java and C++, but the syntax is a little different. Then the only material we still have to cover from the C++ book in this round is the first half of chapter 12 on arrays. That will be done either at the end of this week or the beginning of then next.

There are a few other things about C++ that we will cover in this class, but that will come gradually when we need it. This applies especially to templates, that are in chapter 19 in the C++ book.

Sections this week (25.-26. jan.) will be in the computer rooms and there the solutions to the problems on Weekly notes 2 will covered and you will be helped along with the problems below.

## Problem set 3

Solve the following problems and put the solutions into your section teacher's box before noon monday jan. 31st. They will not accept solutions that arrive later.

1. Write a faster C++ program to calculate the Wolfram sequence, defined in Execise 1 on Weekly note 2. Calculate the value f(100) with this method.
(Hint: Use an array)

2. Write the class Circle that is similar to the class Rectangle in program 10.2, except that the constructor and the method DrawShape now only have the parameter "int radius". Also DrawShape should draw a circle from astrixes (*) with this radius. It doesn't have to be a completely accurate circle, but you might have to use trigonometric functions to get it to look nice.

3. The following C++ function is defined:
```	int& fall( int i )
{
int j = i*2;
return j;
}
```
In a main program the function is called in the following way:
```	int i = fall( 5 ) + fall( 2 );
```
Is there something wrong with this use? If so, explain the problem, but in any case carefully explain the value of the variable i after the execution of the statement.

4. Add to the Cat class on page xxx the method Attacks. It takes as a parameter an object of the class Bird. In 10% of cases the cat catches the bird and then the method prints out something like "Munch, munch", but if the cat doesn't catch the bird, then it prints out "Arg!". It is a random number generator that decides if the cat catches the bird (see a sample program of random number generation). You also have to construct the class Bird, which should be similar to the Cat class, except for different sounds.
Then change the part of the Attacks method that decides whether the cat catches the bird, such that if the cat catches the bird, then it learns from it and the chances that it catches the bird the next time are higher. Make the function such that the probability never reaches 100%. Explain and justify your learning function.

5. In program 10.5 the age and weight of the cat are stored in unnamed variables from the free store that the class variables itsAge and itsWeight point to. Add string variables for the cat's name (itsName) and its owner (itsOwner). Define them in the same way as the age and weight are defined. Add them to the constructor and destructor, and add methods to get the name and owner, and also to change owner.

hh (hja) hi.is, January 24th, 2005.