Using the dateapp.cpp and date.h files. Create an empty project and add these tw
ID: 3676127 • Letter: U
Question
Using the dateapp.cpp and date.h files. Create an empty project and add these two files to it.
1. Define the public methods for the following class in a file date.h:
// class representing a date
class date
{
private:
int month, // 1:January, 2:February, etc.
day, // day of the month
year; // year
bool leap; // a leap year?
bool leapYear ();
public:
date ();
date (int m, int d, int y);
void print ();
void tomorrow ();
void nextMonth ();
int toChristmas ();};
leapYear is a private member function that returns true if the year is a leap year, else it returns false. A year is a leap year if it is divisible by 4 but not divisible by 100. However, if a year is divisible by 400, it is a leap year.
· date is an overloaded constructor. The no parameter version sets the date to January 1, 1, and leap to false. The three parameter version sets the values of the private data variables representing the date equal to the values given by the three parameters. It also properly sets leap to true or false.
· print displays the date in the following format:
month day, year
month should be displayed as the month name. print prints one linefeed before the date.
· tomorrow moves the date ahead one day. It should properly change to the next month and next year when appropriate. If a change is made to a new year, leap should be set correctly.
nextMonth moves the date ahead one month (the day should be the same one month later). Changing to a new year should work along with the proper setting of leap.
toChristmas returns the number of days from current date to Christmas (December 25). The date should not be changed when the function is done running.
There is an application dateapp.cpp written for you. Your program should work for all possible situations not just those in the test program.
dateapp.cpp file
~~~~~~~~~~~~~~~~~~~~~~~
date.h File
~~~~~~~~~~~~~~~~~~~~~
SAMPLE RUN OF PROGRAM
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Five days:
January 30, 2016
January 31, 2016
February 1, 2016
February 2, 2016
February 3, 2016
Days to Christmas: 326
Twelve months:
March 3, 2016
April 3, 2016
May 3, 2016
June 3, 2016
July 3, 2016
August 3, 2016
September 3, 2016
October 3, 2016
November 3, 2016
December 3, 2016
January 3, 2017
February 3, 2017
Days to Christmas: 325
After 100,000 days:
October 17, 274
Days to Christmas: 69
Days to Christmas: 362
Explanation / Answer
date.h
#include <iostream>
#include <string>
using namespace std;
class date
{
private:
int month; // 1:January, 2:February, etc.
int day; // day of the month
int year; // year
bool leap; // a leap year?
bool leapYear ();
public:
date ();
date (int m, int d, int y);
void print ();
void tomorrow ();
void nextMonth ();
int toChristmas ();
};
int monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
string months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
date::date()
{
month=1;
day=1;
year=1;
leap=false;
}
date::date(int m,int d,int y)
{
month=m;
day=d;
year=y;
leap=leapYear();
}
void date::print()
{
cout<<months[month-1]<<" "<<day<<","<<year<<endl;
}
void date::tomorrow()
{
//updating the number of days in february if it is a leap year
if(leap)
{
monthDays[1]=29;
}
else
{
monthDays[1]=28;
}
if((day+1)>31 && month==12)
{
day=1;
month=1;
year=year+1;
leap=leapYear();
}
else if((day+1)>monthDays[month-1])
{
month+=1;
day=1;
}
else
{
day+=1;
}
}
void date::nextMonth()
{
if(month==12)
{
year+=1;
month=1;
leap=leapYear();
}
else
{
month+=1;
}
}
int date::toChristmas()
{
int totalDays=0;
int checkMonth=month;
int checkDay=day;
if(leap)
{
monthDays[1]=29;
}
else
{
monthDays[1]=28;
}
if(month==12 )
{
if(day<=25)
{
return 25-day;
}
else
{
totalDays+=31-day;
checkMonth=1;
checkDay=0;
}
}
//number of days remianing in current month
totalDays+=monthDays[checkMonth-1]-checkDay;
//counting number of days in remaining days except current month and december
for(int i=checkMonth;i<11;++i)
{
totalDays+=monthDays[i];
}
//adding 25 days for december
totalDays+=25;
return totalDays;
}
bool date::leapYear()
{
if(year%400==0)
{
return true;
}
if(year%4==0 && year%100!=0)
{
return true;
}
return false;
}
sample output:
Five days:January 30,2016
January 31,2016
February 1,2016
February 2,2016
February 3,2016
Days to Christmas: 326
Twelve months:March 3,2016
April 3,2016
May 3,2016
June 3,2016
July 3,2016
August 3,2016
September 3,2016
October 3,2016
November 3,2016
December 3,2016
January 3,2017
February 3,2017
Days to Christmas: 325
After 100,000 days: October 17,274
Days to Christmas: 69
Days to Christmas: 362
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.