CSC2402 Assignment 2   Full mark = 100% and is equivalent to 20% of course mark.   Submit your assignment on‐line using the course web page.  Assignment  2  consists  of  five  tasks.  For  each  task,  you  have  to  submit  a  .cpp  file  and  a  documentation file. The .cpp file should have the program code and comments about the algorithm  and the coding. We may compile the .cpp files using MinGW 4.7.1 if required. The documentation  files should contain the compilation messages and sample runs for each program. All files must be in  pure text format. No PDF, HTML, Word files, Open Office files, RTF etc. and no compression or  archiving such as zip, rar, tar etc.  You will submit a single ZIP file (not RAR) which has all of the files  you are required to submit for your assignment – see list below.  If you are using Codelite, the compilation messages can be copied and pasted with the usual crtl‐c  and crtl‐v. For output of sample runs, right click the title bar of the command window, select  EDIT/MARK and then highlight the output. With the output selected, right click the title bar of the  command window, select EDIT/COPY. Position your cursor to a (new) text file opened in a text editor  such as Notepad, and crtl‐v will paste the copied output to the text file.  If you are using MinGW, right click the title bar of the command window; select EDIT/MARK and  then high light the output. With the output selected, right click the title bar of the command  window, select EDIT/COPY. Position your cursor to a (new) text file opened in a text editor such as  Notepad, and crtl‐v will paste the copied output to the text file.  If you are using Linux, you can cut and paste the output on the terminal window into a text such as  vim.   Other files you should download:  1. Sample_protected.cpp  2. Sample_default_arg.cpp  3. Sample_AGE.cpp  4. Sample_DT.cpp  5. Sample_DTG.cpp  6. Sample_WD.cpp  Files you have to submit:   datetime.cpp, datetime.h, task_2_1.txt;   weekday.cpp, weekday.h, task_2_2.txt;   age.cpp, age.h, task_2_3.txt;   days2go.cpp, days2go.h, task_2_4.txt;   date.cpp,  date.h, task_2_5.cpp, task_2_5.txt.      Background information Age There is no standard way to present your age. We will follow the convention detailed below.  Suppose you were born on 5 Aug, 1994.  1. On the day when you were born, you were 1 day old.  2. Within the same month (Aug 1994), your age is calculated in days.   On the 6 Aug 1994, you were (6 – 5 + 1) = 2 days old.   On 27 Aug 1994, you were (27 – 5 + 1) = 23 days old.  3. Within the same year, your age is calculated in months.   On the 2 Sept 1994, you were (9 – 8 + 1) = 2 months old.   On 27 Nov 1994, you were (11 – 8 + 1) = 4 months old.  4. In any year other than your birth year, your age is calculated in years.   On 1 Jan 1995, you were (1995 – 1994 + 1) = 2 years old.   On 25 Dec 2013, you were (2015 – 1994 + 1) = 22 years old.  Note  We ignore the values of hour, minute and second.   We will assume that birthdays (and any other dates we handle) are not on 29 Feb.    The current date is sometime within 2015.    2016 is a leap year.    Due to limitations of  functions, any date before 1970 or after 2020 will not be  considered in our assignments.   Due to time zone differences, it is safer to exclude the date 1 Jan 1970.   You can re‐set the current time of your PC OR you can use this online tool:   Calculate duration between two dates: http://www.timeanddate.com/date/duration.html/  Days before next birthday Assume a current date of  15/04/2015. We can calculate the number of days before your next  birthday:   We ignore the values of hour, minute and second.   We find the days before the current day and the next birthday.   If current day is your birthday, then number of days before next birthday is 0.    If current date (ignoring the year) is before the birth date (ignoring the year), then the  number of days is calculated using the birthday in the current year.   Current date is 15 April 2015, birthday is 20 Aug XXXX, we will calculate the days  from 15 Apr 15 to 20 Aug 15.   If birth date (ignoring the year) is before the current date (ignoring the year), then the  number of days is calculated using the birthday in next year.   Current date is 15 April 2015, birthday is 20 Feb XXXX, we will calculate the days  from 15 Apr 15 to 20 Feb 16.   Since 2016 is a leap year you will need to account for this in your calculations.    Sample calculations using 15 Apr 2015 as the current date:  Birthday  Days  before  next  birthday  Reason (Current date is 15/04/2015)  15/04/1994  0  Today is your birthday! Days before next birthday is 0.   16/04/1994  1  Tomorrow is your birthday! Days before next birthday is 1.  14/04/1994  365  From 15/04/2015 till 14/04/2016: 365 days (2016 is a leap year)  15/05/2000  30  From 15/04/2015 till 15/05/2015:  (April has 30 days)    Pay special attention to leap years.  If you get 365 days for 14/04/2016, the reason is that 2016 is a  leap year.    There are 3 criteria which must be taken into account to identify leap years:   The year is evenly divisible by 4;   If the year can be evenly divided by 100, it is NOT a leap year, unless;   The year is also evenly divisible by 400. Then it is a leap year.  [see: What is a Leap year? http://www.timeanddate.com/date/leapyear.html ]  Protected members Class members can be:   Public members and are accessible by all.   Protected members and are accessible by    Members of the same class   Friends of the class   Members of derived classes    Private members and are accessible by members of the same class.   Note   A class member can access all members of the same class, public, protected or private.   Derived class members can access all public and protected members of the parent class.    The public (e.g. in main()) can only access public members of any class.     See textbook Advanced topic 8.1 on page 356 for more details.  Sample_protected.cpp illustrates the use of protected members.  Default parameters C++ allows you to specify default values for parameters of a function. With default parameter values  set, you can supply less argument to a function than specified. We can omit arguments starting from  the end of the argument list only.   Suppose funct() accepts 3 parameters, the last two assigned with default values:  void funct(int a, char b = 'Z', string c = "Well done!"){ cout << a << endl; cout << b << endl; cout << c << endl; }  You can call funct() with three parameters:  funct(8384, 'A', "My Dear!"); Output expected  8384 A My Dear!   If you call the function with 2 parameters, they must correspond to the first two parameters:  funct(8384, 'A'); Output expected  8384 A Well done!   If you call the function with 1 parameter, it must correspond to the first parameter.  funct(8384); Output expected  8384 Z Well done!   You cannot call the function with no parameter as only (last) two parameters have default  values.  Sample_default_arg.cpp illustrates the case.  Day light saving If your PC is using day light saving, the local time displayed may be different from the sample runs  shown.   When your program is ported to a PC not using day light saving, the display should be the same as  shown in the sample runs.  Day light saving should not affect calculations of age, weekday, and days2go as only year, month and  day are involved.                      Task 1 (20 marks) Given:  class DateTime{ public: DateTime(int y, int m, int d, int h = 0, int min = 0, int s = 0); void display(); protected: string get_string_component(char option, tm* dateStruct); int get_year_days(tm* dateStruct); struct tm DTstruct; private: bool validate_data( int y, int m, int d, int h, int min, int s); }; string DateTime::get_string_component(char component, tm* dateStruct){ char format[3]; format[0] = '%'; format[1] = component; format[2] = '\0'; char ans [80]; strftime(ans, 80, format, dateStruct); string strans(ans); return strans; } int DateTime::get_year_days(tm* DTstruct){ return DTstruct->tm_yday; } Implement member functions   DateTime(int y, int m, int d, int h = 0, int min = 0, int s = 0);     Assign proper values to DTStruct members using the (default) arguments.   y should be a four digit integer and DTstruct.tm_year is a two digit integer.   E.g. user input 1994, DTstruct.tm_year is 94.   E.g. user input 2015, DTstruct.tm_year is 15.   m is from 1 to 12, but DTstruct.tm_mon is from 0 to 11.   d and DTstruct.tm_mday have the same value.   h and DTstruct.tm_hour have the same value.   min and DTstruct.tm_min have the same value.   s and DTstruct.tm_sec have the same value.     DTstruct.tm_wday, DTstruct.tm_yday and DTstruct.tm_isdst will be set to 0 initially.   Re‐adjust values in DTstruct.           void display();   Display the local date and time of DTstruct.   bool validate_data( int y, int m, int d, int h, int min, int s);   return true if all parameter values are all valid;   return false when one or more data are invalid;   a false return will invoke the  exit(EXIT_FAILURE) function call and display  the following error message:  “Incompatible data!”   Validation criteria:     Name  Validation Criteria  y   1970 <= y <= 2020  m  1 <= m <= 12  d  1 <= d <= 31  h  0 <= h <= 23  min  0 <= min <= 59  s  0 <= s <= 59  We can retrieve any one of the date‐time structure member using strftime(). Refer to Assignment 1  for options available.    You have to provide:   a header file datetime.h with code to prevent multiple inclusion.   an class implementation file datetime.cpp.   a text file task_2_1.txt containing compilation messages and sample runs.   Note:   Do not modify the DateTime class public and protected sections. You may add private  members (data/functions) only.   There is no need to implement error exception.  You may use sample_DT_app.cpp to test your implementation. Expected output:  C:\>a 01/02/15 03:04:05 01/02/15 03:04:00 01/02/15 03:00:00 01/02/15 00:00:00 You should also use your own test cases to test for error inputs.  Marking Criteria  Marks  DateTime()  4  display()  4  validate_data()  4  date.h with code to prevent multiple inclusion.  2  date.cpp    4  Compilation messages and sample run outputs (if the program runs correctly)  2    Task 2 (10 marks) Derive class WeekDay from class DateTime such that WeekDay::display() will display local date time  using values from the parameters and the corresponding weekday.  For a handy online weekday  calculator:  What Day is this Date? http://www.timeanddate.com/date/weekday.html  class WeekDay : public DateTime { public: WeekDay(int y, int m, int d, int h = 0, int min = 0, int s = 0); void display(); }; Implement   WeekDay(int y, int m, int d, int h = 0, int min = 0, int s = 0);   Validate the parameter values the same ways as in DateTime().   void display();   display the local date time and the corresponding weekday.    You have to provide:   a header file weekday.h with code to prevent multiple inclusion.   an class implementation file weekday.cpp.   a text file task_2_2.txt containing compilation messages and sample runs.   Note   You have to include the code for class DateTime in weekday.h and weekday.cpp.   Do not modify DateTime and WeekDay class public and protected sections. You may add  private members (data/functions) only.   There is no need to implement error exception.  You may use sample_WD_app.cpp to test your implementation. Expected output:  C:\>a 01/02/15 03:04:05 is Friday 01/02/15 03:04:00 is Friday 01/02/15 03:00:00 is Friday 01/02/15 00:00:00 is Friday You should also use your own test cases to test for error inputs.  Marking Criteria  Marks  WeekDay()  2  display()  2  weekday.h with code to prevent multiple inclusion.  2  weekday.cpp    2  Compilation messages and sample run outputs (if the program runs correctly)  2    Task 3 (20 marks) Derive class Age from class DateTime such that Age::display() will display birthdate in local date time  format using values from the parameters, the current time and the age.  class Age : public DateTime { public: // Date supplied is birth date, should be earlier than current date Age(int y, int m, int d, int h = 0, int min = 0, int s = 0); void display(); protected: struct tm Astruct; // tm for current time private: string int_2_string(int); void error_action(); string age; }; string Age::int_2_string(int n){ ostringstream outstr; if (n > 100000){ outstr << (n - 100000); return outstr.str() + " days old."; } else if (n > 1000){ outstr << (n - 1000); return outstr.str() + " months old."; } else { outstr << n; return outstr.str() + " years old."; } } void Age::error_action(){ cout << "Birthday must be in the past!\n"; exit(EXIT_FAILURE); } Implement   Age(int y, int m, int d, int h = 0, int min = 0, int s = 0);   Validate parameter values the same way as in class DateTime.   When calculating age, the unit can be day, month or year. To differentiate the units,  add 1,000 to age in months, add 100,000 to age in days. When passed to  int_2_string, we can convert the age to proper values and units.   void display();   Display the local date time created as birthdate; the current date and the age  calculated using the birthdate and the current date.  You have to provide:   a header file age.h with code to prevent multiple inclusion.   an class implementation file age.cpp.   a text file task_2_3.txt containing compilation messages and sample runs.   Note    You have to include the code for class DateTime in age.h and age.cpp.   You may include the code for class WeekDay in age.h and age.cpp.   Do not modify DateTime and Age class public and protected sections. You may add private  members (data/functions) only.   There is no need to implement error exception.  You may use sample_AGE_app.cpp to test your implementation. Expected output:  C:\>a Birthday: 12/25/94 03:04:05 Current date: 04/15/15 15:17:15 Age is 22 years old. Birthday: 12/25/94 03:04:00 Current date: 04/15/15 15:17:15 Age is 22 years old. Birthday: 12/25/94 03:00:00 Current date: 04/15/15 15:17:15 Age is 22 years old. Birthday: 12/25/94 00:00:00 Current date: 04/15/15 15:17:15 Age is 22 years old. Birthday: 04/15/15 00:00:00 Current date: 04/15/15 15:17:15 Age is 1 days old. Birthday: 04/01/15 00:00:00 Current date: 04/15/15 15:17:15 Age is 15 days old. Birthday: 03/15/15 00:00:00 Current date: 04/15/15 15:17:15 Age is 2 months old. Birthday: 12/25/14 00:00:00 Current date: 04/15/15 15:17:15 Age is 2 years old. Birthday: 01/25/14 00:00:00 Current date: 04/15/15 15:17:15 Age is 2 years old. You should also use your own test cases to test for error inputs.  Marking Criteria  Marks  Age() and display()  12  age.h with code to prevent multiple inclusion.  2  age.cpp    4  Compilation messages and sample run outputs (if the program runs correctly)  2    Task 4 (20 marks) Derive class Days2Go from class DateTime such that Days2GoAge::display() will display birthdate in   local date time format using values from the parameters and the number of days before your next  birthday. The current date used in the sample run is assumed to be 15/04/2015.  class Days2Go : public Age { public: Days2Go(int y, int m, int d, int h = 0, int min = 0, int s = 0); void display(); private: int days; }; Implement   Days2Go(int y, int m, int d, int h = 0, int min = 0, int s = 0);   2016 is a leap year.    Birth year may be a leap year. You have to consider 29 Feb.   void display();   Display the local date time created as birthdate; the current date and the age  calculated using the birthdate and the current date.  You have to provide:   a header file days2go.h with code to prevent multiple inclusion.   an class implementation file days2go.cpp.   a text file task_2_4.txt containing compilation messages and sample runs.   Note    You have to include the code for class DateTime and class Age in days2go.h and  days2go.cpp.   Do not modify class DateTime, Age and Days2Go public and protected sections. You may add  private members (data/functions) only.   There is no need to implement error exception.  You may use sample_DTG_app.cpp to test your implementation. Assuming the current date is  15/04/2015, expected output:  C:\>a Birthday: 04/14/94 00:00:00 Current date: 04/15/15 15:32:15 Days before next birthday: 364 Birthday: 04/15/94 00:00:00 Current date: 04/15/15 15:32:15 Days before next birthday: 0 Birthday: 04/16/94 00:00:00 Current date: 04/15/15 15:32:15 Days before next birthday: 1 Birthday: 05/15/00 00:00:00 Current date: 04/15/15 15:32:15 Days before next birthday: 30   You should also use your own test cases to test for error inputs.    Marking Criteria  Marks  Days2Go()  6  display()  4  days2go.h with code to prevent multiple inclusion.  2  days2go.cpp    4  Compilation messages and sample run outputs (if the program runs correctly)  2      Task 5 (30 marks) Write a C++ program that stores information for date‐time objects including DateTime, WeekDay,  Age, and Days2Go objects. The program will display all the object information using the display()  function provided by the objects.  Note   Use a single vector to store information for all objects. Arrays, and other data structures are  not allowed.    When displaying information, the corresponding display() function of the object should be  used.   E.g. DateTime object will display local date time and Age object will display  birthdate, current date and age.   You may have to modify the class interface(s) to enable polymorphism.     There is no need to implement error exception.   Adjust current date to 15 Apr 2015.   You have to submit date.h, date.cpp, task_2_5.cpp and task_2_5.txt containing compilation  messages and sample runs.    Dates to be stored:   DateTime: 1990, Jan, 7, 14:15:16  DateTime: 2000, Mar, 14, 14:15  DateTime: 2015, Apr, 15, 14 hour  DateTime: 2015, Apr, 10  DateTime: 2015, Feb, 16  DateTime: 2000, Feb, 16            WeekDay: 1990, Jan, 7, 14:15:16  WeekDay: 2000, Mar, 14, 14:15  WeekDay: 2015, Apr, 15, 14 hour  WeekDay: 2015, Apr, 10  WeekDay: 2015, Feb, 16  WeekDay: 2000, Feb, 16            Age: 1990, Jan, 7, 14:15:16  Age: 2000, Mar, 14, 14:15  Age: 2015, Apr, 15, 14 hour  Age: 2015, Apr, 10  Age: 2015, Feb, 16  Age: 2000, Feb, 16            Days2Go: 1990, Jan, 7, 14:15:16  Days2Go: 2000, Mar, 14, 14:15  Days2Go: 2015, Apr, 15, 14 hour  Days2Go: 2015, Apr, 10  Days2Go: 2015, Feb, 16  Days2Go: 2000, Feb, 16  …              Expected output: (current date is 15 Apr 2015)   01/07/90 14:15:16 03/14/00 14:15:00 04/15/15 14:00:00 04/10/15 00:00:00 02/16/15 00:00:00 02/16/00 00:00:00 01/07/90 14:15:16 is Sunday 03/14/00 14:15:00 is Tuesday 04/15/15 14:00:00 is Wednesday 04/10/15 00:00:00 is Friday 02/16/14 00:00:00 is Saturday 02/16/00 00:00:00 is Wednesday Birthday: 01/07/90 14:15:16 Current date: 04/15/15 10:21:59 Age is 26 years old. Birthday: 03/14/00 14:15:00 Current date: 04/15/15 10:21:59 Age is 16 years old. Birthday: 04/15/15 14:00:00 Current date: 04/15/15 10:21:59 Age is 1 days old. Birthday: 04/10/15 00:00:00 Current date: 04/15/15 10:21:59 Age is 6 days old. Birthday: 02/16/15 00:00:00 Current date: 04/15/15 10:21:59 Age is 3 months old. Birthday: 02/16/00 00:00:00 Current date: 04/15/15 10:21:59 Age is 16 years old. Birthday: 01/07/90 14:15:16 Current date: 04/15/15 10:21:59 Days before next birthday: 267 Birthday: 03/14/00 14:15:00 Current date: 04/15/15 10:21:59 Days before next birthday: 333 Birthday: 04/15/15 14:00:00 Current date: 04/15/15 10:21:59 Days before next birthday: 0 Birthday: 04/10/15 00:00:00 Current date: 04/15/15 10:21:59 Days before next birthday: 360 Birthday: 02/16/15 00:00:00 Current date: 04/15/15 10:21:59 Days before next birthday: 307 Birthday: 02/16/00 00:00:00 Current date: 04/15/15 10:21:59 Days before next birthday: 307 … You should also use your own test cases to test for error inputs.  Marking Criteria  Marks  Use of vector  5  Use of iterator  5  Storing data  5  Retrieving data  5  Polymorphic form of display()  5  Compilation messages and sample run outputs (if the program runs correctly)  5    **** End of assignment 2 ****