You are on page 1of 2

KIE1008 (Programming Tutorial) Session 2016/2017 (Semester 2)

Tutorial 2: Object-Oriented Programming (Polymorphism and Operator Overloading)

1. Create a class Person that contains member name. Create three constructor functions:
Person(), Person (name) and Person(const Person&). Also create the getName()
and setName() functions. Overload the following three operators: =, >> and <<.

Create a base class called vehicle that has the manufacturer’s name, number of cylinders in
the engine, and owner (type Person).

Then, create a class called Truck that is derived from Vehicle and has additional properties,
the load capacity in tons and towing capacity in pounds. Your classes should have a reasonable
complement of constructors, accessor and mutator functions, and overloaded assignment
operators, and a copy constructor.

Write a main function to tests all functions.

2. (Predator-prey simulation) In this simulation, the prey are mice and the predators are snakes.
These animals live in a 20 x 20 grid of cells. Only one animal may occupy a cell at a time. The grid
is enclosed, so an animal is not allowed to move off the edges of the world. Time is simulated in
steps.
The behavior of mice:
i. Move: For every time step, the mice randomly try to move up, down, left, or right. If the
new cell is empty, the mouse moves to the new location. If the neighboring cell is
occupied or would move off the grid, then the mouse stays in the current cell.
ii. Breed: If a mouse survives for two time steps, the mouse will breed at the end of the time
step (i.e., after moving). This is simulated by creating a new mouse (only one) in an
adjacent (up, down, left, or right) cell that is empty. If there is no empty cell available, no
breeding occurs. Once an offspring is produced, a mouse cannot produce an offspring
again until it has survived two more time steps.

The behavior of snakes:


i. Move: For every time step, the snake will move to an adjacent cell containing a mouse
and eat the mouse. If there are more than one adjacent cell contains mouse, the snake
will randomly choose one of the mice to eat. If there are no mice in adjacent cells, the
snake moves according to the same rules as the mouse. Note that a snake cannot eat
other snake.
ii. Breed: If a snake survives for five time steps, at the end of the time step it will spawn off
a new snake in the same manner as the mouse.
iii. Starve: If a snake has not eaten for three time steps, at the end of the third time step it
will starve and die (removed from the grid of cells).

During one turn, all the snakes should move before the mice.

Write a program to implement this simulation and draw the world using “O” for mice and “X” for
snakes.
KIE1008 (Programming Tutorial) Session 2016/2017 (Semester 2)

Create a class named Organism that encapsulates basic data common to both mice and snakes.
This class should have a virtual function named move that is defined in the derived classes of
Mouse and Snake.

You may need additional data structures to keep track of which animals have moved.

Initialize the world with 10 snakes and 50 mice at random locations.

After each time step, show the world (as below) and give the population of each animal. Prompt
the user to press “Enter” to move to the next time step.

Simulation ends after 20 times steps, or either one of the animals has become extinct.

You should see a cyclical pattern between the populations of predators and prey, although
random perturbations may lead to the elimination of one or both species.

O X - - O - - - - - O - - - - - - - - -
- - - - - - - - - - - - - - - - - O O O
- - - - - - - O - - - O O X - - - - - -
- - - - - - - - - - - - - - - - - O - -
- - - - - - O - - - - - - - - - - - - -
O O O X - - - - O - - - X O - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - O X - - - - O - - - - - - - - -
- - O - - - - - - - - O - - - - - O O O
- - - - O - - O - O - - - - - - - - - -
- - - - - - - - - - - - O - - X - - - -
- - - - - - - X O - - - - - - - - - - -
O - - O - - - - - O - O - - O O - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - O - - - X - - - - - O - - -
- - X - - - - - - - - - - - - O - - - -
- - - - - O - - O - - - - O - - - - - -
O - - - - - - - - - - O - - - X - - - -
- - O - - - O - - - - - - - - - - - O O
- - - - - - - O - - - O - - - - - - O O

Time Step 0: 10 snakes and 50 mice.

--END--

You might also like