Please create a C++ class called myDate. It should have the following operations
ID: 3587372 • Letter: P
Question
Please create a C++ class called myDate. It should have the following operations:
myDate() – default constructor. This will set the date to May 10, 1959.
myDate(int M, int D, int Y) – overloaded constructor. This will set the date to the values passed in through the parameter list represented by Month, Day and Year.
void display() – display the date in the following format (May 11, 1959) Do NOT print a linefeed after the date.
void incrDate(int N) – increment the date by N days.
void decrDate(int N) – decrement the date by N days.
int daysBetween(myDate D) – return the number of days between this date and the date D. If date D is a future date, the return value will be a positive int. If date D is in the past, the return value will be a negative int.
int getMonth() – return the month in integer form
int getDay() – return the day of the month
int getYear() – return the year
int dayOfYear() - return the number of days since the current year began. Example Jan 1 is 1, Feb 1 is 32.
string dayOfWeek() – returns Monday, Tuesday, etc according to the day of the week.
Consider the overloaded constructor, if any invalid data is passed to this constructor, ignore all data and set the values to the default date.
Explanation / Answer
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
class MyDate{
public: int day;
public: int month;
public: int year;
std::string myString[12] =
{
"Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
};
public: MyDate(){
day=11;
month=4;
year=1959;
}
public: MyDate( int D,int M, int Y) {
day=D;
month=M;
year=Y;
}
public:void display(){
cout<<myString[month] <<" "<< day<<", " << year;
}
public:int getMonth(){
return month;
}
public:int getDay(){
return day;
}
public:int getYear(){
return year;
}
public : void setDate(int d,int m,int y){
day=d;
month=m;
year=y;
}
public:void incrDate(int d){
int totalDay=getDay()+d;
static int increment[12] = { 1, -2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 };
if(totalDay>30&&increment[getMonth()]==0){
if(getMonth()+1>11)
setDate(totalDay-30,getMonth()+1-11,getYear()+1);
else
setDate(totalDay-30,getMonth()+1,getYear());
}
else if(totalDay>31&&increment[getMonth()]==1){
if(getMonth()+1>11)
setDate(totalDay-31,getMonth()+1-11,getYear()+1);
else
setDate(totalDay-31,getMonth()+1,getYear());
}
else{
setDate(totalDay,getMonth(),getYear());
}
cout<<" days= "<<getDay() <<" month = "<<getMonth()<<" year = "<<getYear();
}
public:void decrDate(int d){
int totalDay=getDay()-d;
static int increment[12] = { 1, -2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 };
if(totalDay>0){
setDate(totalDay,getMonth(),getYear());
}
else{
if(getMonth()==0){
setDate(31-totalDay,11,getYear()-1);
return;
}
if(increment[getMonth()]==1)
setDate(31-totalDay,getMonth()-1,getYear());
else
setDate(30-totalDay,getMonth()-1,getYear());
}
cout<<" days= "<<getDay() <<" month = "<<getMonth()<<" year = "<<getYear();
}
public: int dayOfYear(){
//static int increment[12] = { 1, -2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 };
int dayOfYear=getDay();
int m=getMonth();
//Leap year
if(getYear()%4==0)
dayOfYear+=((m)*30)-1;
else
dayOfYear+= ((m)*30)-2;
if(m==0)
dayOfYear+=1 ;
else if(m==2)
dayOfYear+=2;
else if(m==4)
dayOfYear+=3;
else if(m==7)
dayOfYear+=4;
else if(m==9)
dayOfYear+=5;
else if(m==11)
dayOfYear+=6;
return dayOfYear;
}
public: int daysBetween(MyDate newDate){
static int increment[12] = { 1, -2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 };
int daysInc = 0;
if (newDate.getDay() - getDay() < 0)
{
int month = newDate.getMonth() - 2; // -1 from zero, -1 previous month.
if (month < 0)
month = 11; // Previous month is December.
daysInc = increment[month];
if ( (month == 1) && (newDate.getYear()%4 == 0) )
daysInc++; // Increment days for leap year.
}
int total1 = getYear()*360 + getMonth()*30 + getDay();
int total2 = newDate.getYear()*360 + newDate.getMonth()*30 + newDate.getDay();
int diff = total2 - total1;
int years = diff/360;
int months = (diff - years*360)/30;
int days = diff - years*360 - months*30 + daysInc;
// Extra calculation when we can pass one month instead of 30 days.
if (getDay() == 1 && newDate.getDay() == 31) {
months--;
days = 30;
}
cout<<" days = "<<days <<"month = "<<months<<"year = "<<years;
return days;
}
public: string dayofweek(int d, int m, int y)
{
std::string arrDayweek[7] =
{
"Sunday", "Monday", "Tusday","Wednesday","Thusday","Friday","Saterday"
};
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= m < 3;
int weekDay=( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
return arrDayweek[weekDay];
}
};
int main()
{
MyDate mj;
mj.display();
MyDate newDate(5,4,1959);
// mj.daysBetween(newDate);
cout<<" day of year = "<< mj.dayOfYear();
cout<<" day of week= "<<mj.dayofweek(mj.getDay(),mj.getMonth(),mj.getYear());
// mj.incrDate(21);
/* cout<<" month : "+mj.myString[mj.getMonth()];
cout<<" day : "<<mj.getDay();
cout<<" year : "<<mj.getYear();
cout<<" year : "<<newDate.getYear();*/
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.