Your task today is to finish the Date class. The class has been started for you
ID: 3808511 • Letter: Y
Question
Your task today is to finish the Date class. The class has been started for you already and the purpose of the main program is to test all of the various functions of the class.
The header file has all of the information about what each function is supposed to do.
Look for the TODO comments in the Date.cpp file for where you will need to make changes.
The challenge of this lab is understanding the code that you have been given as much as it is writing your new code.
If you get stuck then see the hints.
Submit a cpp file containing the implementation of the Date class.
#ifndef DATE_H
#define DATE_H
#include
class Date
{
private:
int month;
int day;
int year;
const int
MIN_YEAR = 0, MAX_YEAR = 3000,
MIN_DAY = 1, MAX_DAY = 31,
FEB_MAX_LEAP_YEAR = 29, FEB_MAX = 28,
MIN_MONTH = 1, MAX_MONTH = 12;
public:
/************************************
** Constructors **
*************************************/
// Sets day to 1, month to 1, and year to 2000
Date();
// Recieves: Month, Day, Year (optional)
// Returns: new object
Date(int, int, int = 0);
/************************************
** Accessors **
*************************************/
// Returns the year
int getYear();
// Returns the month
int getMonth();
// Returns the day
int getDay();
// Recieves: Nothing
// Returns: month/day/year
// Example: 3/28/2009
// If year is 0 then returns month/day
// Example: 12/25
std::string getDateShort();
// Recieves: Nothing
// Returns: MonthName day, year
// Example: March 28, 2009
// If year is 0 then returns MonthName day
// Example: December 25
std::string getDateLong();
/************************************
** Mutators **
*************************************/
// Recieves: year (0 to 3000)
// Returns: nothing
// If the value is not within the correct range, year will be set to 0.
// A value for year of 0 indicates a date that applies to more than one year.
// For example, the date for Halloween is October 31.
// Whenever you set the year, it is possible to change the range of the day.
// Therefore this function calls setDay with the current value of day if the year has been set.
void setYear(int);
// Recieves: month (1 to 12)
// Returns: nothing
// If the value sent is not within the correct range, the month is set to 1
// After setting the month, the setDay function is called with the current value of day
void setMonth(int);
// Recieves: day (1-31 with restrictions)
// Returns: Nothing
// If the value is within the specified range for the month, sets the value of day to the one sent
// otherwise the day is sent to 1.
// For months 1, 3, 5, 7, 8, 10, and 12 the day can be 1 - 31.
// For months 4, 6, 9, and 11 the day can be 1 - 30.
// For month 2 it depends on the year:
// The day for month 2 can be 1 - 28 except on leap year.
// The day for month 2 can be 1 - 29 on leap years.
// A leap year is when the year is divisible by 4.
// When a year is divisible by 100 but not divisible by 400, it is not a leap year.
void setDay(int);
// Recives: Month, Day, Year
// Returns: Nothing
// Sets the date to the given values, assuming that the given date meets the day, month and year validation requirements
void setDate(int, int, int = 0);
};
#endif
Just finish the parts where the comments say to finish
Explanation / Answer
To begin with the program, I have created a CPP file which includes the Date class along with the comments:
Code:-
// The header file declaration
#include < stream.h>
#include < stdlib.h >
#include < math.h >
// function to check max value
int checkMaxValue (int p, int q)
{
if (p>q) return(p) ; else return (q);
}
// function to check min value
int checkMinValue (int p, int q)
{
if (p>q) return(q); else return (p);
}
// create class Date
class Date
{
public:
Date ();
// It is a constructor function
Date ( int mn, int day, int yr);
// It is used for displaying date
void displayDate();
int MonthDetails();
void MonthDetailSet(int mn);
~Date();
private:
int month, day, year;
int DatesFarList();
};
// constructor definition
Date::Date ()
{
month = day = year = 1;
}
Date::Date (int mn, int dy, int yr)
{
static int lengthSize[] = { 0, 28, 31, 28, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
month = max(1, mn);
month = min(month,12);
day = max(1,dy);
day = min(day, lengthSize[month]);
year = max(1, yr);
}
void Date::displayDate()
{
static char *monthName[] = {"Null", "January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December" };
cout << ' ' << monthName[month] << ' ' << day << "," << year << ' ';
cout << "The days so far list to be displayed: " << DatesFarList() << ' ';
}
Date::~Date()
{
cout << "Its glad to see you using the Date service. ";
}
int Date::DatesFarList()
{
int total = 0;
static int lengthSize[] = { 0, 28, 31, 28, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (int i=1; i < month; i++) total += lengthSize[i];
total += day;
return (total);
}
int Date::MonthDetails()
{
return month;
}
void Date::MonthDetailSet(int mn)
{
month = checkMaxValue(1, mn);
month = checkMinValue(month, 12);
}
// Main method declaration
void main()
{
Date mydate(3, 3, 1995);
Date date2(1,5,1996);
Date date4;
Date *date3;
Date *date6;
Date date5(1,1,1);
date3 = new Date(45,45,44545);
mydate.displayDate();
date2.displayDate();
date3->displayDate();
cout << "GOOD: " << date3->MonthDetails() << ' ';
date3->MonthDetailSet(4);
cout << "GOOD: " << date3->MonthDetails() << ' ';
delete(date3);
date6 = new Date(1,1,1);
cout << "GOOD: " << date3->MonthDetails() << ' ';
date3->MonthDetailSet(4);
cout << "GOOD: " << date3->MonthDetails() << ' ';
date3->displayDate();
date4.displayDate();
date5.MonthDetailSet(3);
delete(date6);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.