Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C++ class called myDate. It should have the following operations: myDat

ID: 3923744 • Letter: C

Question

Create a C++ class called myDate.

It should have the following operations:

myDate() – default constructor. This will set the date to May 11, 1959 (A very important
date!!!)

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 getYearOffset() – return the number of days since the current year began.

Write a driver program that tests each operation

Explanation / Answer

#include <iostream>

using namespace std;

class MyDate{
int day;
int month;
int year;
  
string getMonthString()
{
string monthStr = "";
switch(month)
{
case 1:
monthStr = "January";
break;
case 2:
monthStr = "February";
break;
case 3:
monthStr = "March";
break;
case 4:
monthStr = "April";
break;
case 5:
monthStr = "May";
break;
case 6:
monthStr = "June";
break;
case 7:
monthStr = "July";
break;
case 8:
monthStr = "August";
break;
case 9:
monthStr = "September";
break;
case 10:
monthStr = "October";
break;
case 11:
monthStr = "November";
break;
case 12:
monthStr = "December";
break;
}
return monthStr;
}
public:
MyDate()
{
day = 11;
month = 5;
year = 1959;
}
  
MyDate(int day, int month, int year)
{
this->day = day;
this->month = month;
this->year = year;
}
  
void display()
{
cout<<getMonthString()<<" "<<this->day<<", "<<this->year<<endl;
}
  
void incrDate(int n)
{
if(this->day + n > 30)
{
this->day = this->day + n % 30;   
if(this->month + 1 > 12)
{
this->month = 1;
this->year++;
}   
else
this->month++;
}
else
{
this->day+=n;
}
}
  
void decrDate(int n)
{
if(this->day - n < 0)
{
this->day = 30 - n + this->day;   
if(this->month - 1 == 0)
{
this->month = 12;
this->year--;
}   
else
this->month--;
}   
else
{
this->day -= n;
}
}
  
int daysBetween(MyDate d)
{
int diffYear = d.year - this->year;
int diffDays = 0;
if(diffYear != 0)
{
diffDays += 365*diffYear;
}
  
int diffMonth = d.month - this->month;
if(diffMonth != 0)
{
diffDays += 30*diffMonth;
}
  
diffDays += d.day - this->day;
  
return diffDays;
}
  
int getDay()
{
return this->day;
}
  
int getMonth()
{
return this->month;
}
  
int getYear()
{
return this->year;
}
  
int getYearOffset()
{
int offset = 0;
offset += (this->month - 1)*30;
offset += this->day;
return offset;
}
  
};

int main() {
MyDate myDate;
myDate.display();
myDate.incrDate(2);
myDate.display();
myDate.decrDate(15);
myDate.display();
MyDate myDate2;
cout<<"Days between "<<myDate2.daysBetween(myDate)<<endl;
return 0;
}