2012年2月21日 星期二

Exercise #7


This assignment will give you practice working with classes, constructors, static data member, static member functions, and friend functions. The goal of this assignment is to create a partial model of a population system, emulating the aging and dying of a population, but unfortunately not the birth process. But, maybe that's fortunate for the programmer

Program requirements:
  • Create the three classes described below.
  • Use the main() function included.
  • Your output should look like that shown below.
  • Your program must be divided into multiple files. Each class should be defined in a separate header file and the member functions for each class should be in a separate source file.  main() should also be in a separate file. Your program should consist of at least 7 files. Print each file on a separate page.
Class Descriptions
The date class is used to represent calendar dates. As a minimum, the class must contain:
  • 3 unsigned int data members to represent month, day, and year
  • a default constructor (use the same one from assignment 4)
  • a constructor that takes 3 unsigned ints as arguments
  • an increment function that adds 1 day to the date (you can use the same one from assignment 3. For determining ages, you can assume that a year is 365.25 days long.
  • a display() _pass() function that adds a random number of days to a date. The random number sfunction that displays the date in the mm/dd/yy format
  • a let_timehould be between 1 and 365. You will be using this function to add days to the (global) TODAY date. Hint:  you might use the random number to call the increment() function repeatedly.
  • Declare the human class as a friend of the date class.
  • Declare the function, int  difference_between_2_dates(date,date) as a friend of the date class.
The human class must contain at least:
  • 3 data members:
  • char    name[32]
    date    birthday
    bool    alive
  • 2 static data members
  • static human*    oldest_human
    static unsigned long    number_of_living_humans
  • a constructor:    human(const char* n,const date& b)
  • necessary accessor functions
  • a function, age() that returns a human's age in years
  • a die() function (you know what that means)
  • a display() function
  • a static member function that assigns the appropriate human* to the oldest_human.
  • a static member function that returns the number_of_living_humans
  • Declare the function void population::display() const as a friend of the human class.
The population class must contain:
  • Two data members:
    • human**    people
    • const unsigned long    original_size
  • A private member function:     determine_oldest() that "sets" the oldest_human
  • A constructor
  • A destructor
  • A display() function
  • An examine_population() function that takes a look at the population, calls the following roll_the_dice() function for each "living" human.  If roll_the_dice() returns a number greater than .5, the human should "die".
float roll_the_dice(unsigned short age)
{
    return (float) age*(rand()%100)/10000.;
}

Assumptions
  • All humans were "born" in the last century.
  • Use the difference_between_2_dates() function so that it always returns a positive value. That is, you should always subtract the older date from the newer date.
  • Use a population size of 20 for the final testing of your program.
Hints and Suggestions
  • If you are new to working with multiple files, keep your program as one file until you get it working, then try to split it into multiple files.
  • Declare TODAY as a global variable.  To do this, enter:
  • date TODAY;
    in your main() source file and declare it as an extern in the date header file, like this:
    extern date TODAY;
  • Create a global array of names that you can use in the population constructor, like this:
  • char* NAMES[] = {"Fred","Sam","Sally","George","Sue","Mary","Bill",...};
  • Do not work with a population size of 20 until you are sure that your program is working. Use a small population, like 4 or 5.
The main() function

int main()
{
    srand(time(0));            // seed the random number generator
    population World(POPULATION_SIZE);
    cout << "Today is ";
    TODAY.display();
    cout << endl;
    World.display();

    // let time pass until half of the world's population dies
    do
{
        TODAY.let_time_pass();   
        World.examine_population();    // record deaths, find oldest
    } while (human::get_number_of_living_humans() > POPULATION_SIZE/2);

    cout << "Today is ";
    TODAY.display();
    cout << endl;
    World.display();
    return 0;
}

Sample Output

Today is 11/22/03

Allen was born on 9/15/04 is 99
Catherine was born on 11/1/62 is 41
Naihui was born on 10/16/56 is 47
Gayatri was born on 9/26/24 is 79
Bin was born on 5/12/28 is 75
Evan was born on 5/2/82 is 21
Sandy was born on 3/8/50 is 53
Sridevi was born on 4/26/85 is 18
Tanya was born on 3/26/32 is 71
Jing was born on 9/18/25 is 78
Haiying was born on 7/24/75 is 28
Rose was born on 1/21/25 is 78
Nisha was born on 5/8/72 is 31
Hnin was born on 3/14/68 is 35
Adeline was born on 8/17/57 is 46
Chen Wei was born on 8/8/19 is 84
Joe was born on 7/6/86 is 17
Bob was born on 11/26/33 is 69
Mary was born on 7/8/41 is 62
Sue was born on 4/7/80 is 23
The oldest living person, Allen is 99 years old.

4/9/04 Allen died at the age of 99
4/9/04 Chen Wei died at the age of 84
4/9/04 Bob died at the age of 70
9/16/04 Sandy died at the age of 54
9/16/04 Tanya died at the age of 72
9/16/04 Rose died at the age of 79
9/16/04 Mary died at the age of 63
1/10/05 Bin died at the age of 76
8/5/05 Gayatri died at the age of 80
8/5/05 Jing died at the age of 79
Today is 8/5/05

Catherine was born on 11/1/62 is 42
Naihui was born on 10/16/56 is 48
Evan was born on 5/2/82 is 23
Sridevi was born on 4/26/85 is 20
Haiying was born on 7/24/75 is 30
Nisha was born on 5/8/72 is 33
Hnin was born on 3/14/68 is 37
Adeline was born on 8/17/57 is 47
Joe was born on 7/6/86 is 19
Sue was born on 4/7/80 is 25
The oldest living person, Catherine is 42 years old.

沒有留言:

張貼留言