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

Please write C++ program following the below instructions. THANKS!! Your program

ID: 3737662 • Letter: P

Question

Please write C++ program following the below instructions. THANKS!!

Your program should implement the following: 1. Date class Implement a Date class that has the following private member variables: month of type int day of type int year of type int monthName, a static array of strings which will be initialized to the months' names "January", "February", etc. and the following public member functions: . set (month_, day_, year_) mutator that sets the variables month, day and year to the parameter values overloaded

Explanation / Answer

Date.cpp

#include "Date.h"

// Set function as given in question.
void Date::set(int _month, int _date, int _year) {
   month = _month;
   date = _date;
   year = _year;
}
// static month names
string Date::months[] = {"January", "February", "March", "April", "May", "June", "July","August", "September", "October", "November", "December"};

bool Date::operator<(Date &d) {
   // Make bigger number with dates and then compare the value.
   int d1 = d.date + d.month*100 + d.year * 10000;
   int d2 = this->date + this->month*100 + this->year * 10000;

   if(d1 < d2)
       return true;
   return false;
}

bool Date::operator==(Date &d) {
   // check if the date month and year are the same.
   if(d.date == this->date &&
           d.month == this->month &&
           d.year == this->year)
       return true;

   return false;

}

bool Date::operator<=(Date &d) {

   // using the already overloaded operators
   if( *(this) < d || *(this) == d)
       return true;
   return false;  
}

const int monthDays[12] = {31, 28, 31, 30, 31, 30,
                           31, 31, 30, 31, 30, 31};

int Date::operator-(Date &d) {
   long int n1 = d.year*365 + d.date;


   // Add days for months in given date
   for (int i=0; i<d.month - 1; i++)
       n1 += monthDays[i];


   // SIMILARLY, COUNT TOTAL NUMBER OF DAYS BEFORE 'dt2'

   long int n2 = this->year*365 + this->date;
   for (int i=0; i<this->month - 1; i++)
       n2 += monthDays[i];

   // return difference between two counts
   return (n2 - n1);
}

void Date::print() {
   cout<<months[month-1]<<" "<<date<<", "<<year<<endl;
}

Date header file as given in the question.

Date.h

#ifndef __DATE_H__
#define __DATE_H__

#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>


using namespace std;

class Date{
   int month, date, year;
   static string months[];

   public:
   void set(int _month, int _date, int _year);
   bool operator<(Date &d);
   bool operator<=(Date &d);
   bool operator==(Date &d);
   int operator-(Date &d);
   int countLeapYears();
   void print();
};

#endif

Main program as given in the question.

main.cpp

#include "Date.h"

int getMonth(string d) {
   string ms = d.substr(0,2);
   const char *m = ms.c_str();
   return atoi(m);
}

int getDay(string d) {
   string ms = d.substr(3,2);
   const char *m = ms.c_str();
   return atoi(m);
}
int getYear(string d) {
   string ms = d.substr(6,string::npos);
   const char *m = ms.c_str();
   return atoi(m);
}

int main() {
   const int NUM_DATES = 4;

   string dateArray[NUM_DATES] = {"02/12/2100","02/12/2200", "02/13/2100","03/12/2100"};

   cout<< "************************** ";
   cout<< "Driver for the Date class: ";
   cout<< "************************** ";

   Date d1,d2;
   for(int i = 0; i<NUM_DATES; i++) {
       for(int j = 0; j<NUM_DATES; j++) {
           int month = getMonth(dateArray[i]);
           int day = getDay(dateArray[i]);
           int year = getYear(dateArray[i]);
           d1.set(month, day, year);

           cout<<" ===================================== ";
           cout<<"d1: ";
           d1.print();
           month = getMonth(dateArray[j]);
           day = getDay(dateArray[j]);
           year = getYear(dateArray[j]);
           d2.set(month, day, year);
           cout<<"d2: ";
           d2.print();
           cout<<endl;

           if(d1 < d2)
               cout<< "(d2 < d1) ";
           if(d2 < d1)
               cout<< "(d2 < d1) ";
           if(d2 == d1)
               cout<< "(d2 == d1) ";
           if(d1 <= d2)
               cout<< "(d1 <= d2) ";
           if(d2 <= d1)
               cout<< "(d2 <= d1) ";

           cout<< "(d2 - d1 = " << d2 - d1 << " days)" <<endl;
          
       }
   }
}