Prog Tutorial2 PDF
Prog Tutorial2 PDF
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.
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.
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.
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
--END--