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

In C++ no pointers: Write a program that defines a class Date and implement it a

ID: 3809366 • Letter: I

Question

In C++ no pointers:   

Write a program that defines a class Date and implement it as required. The class Date should consist of three private member variables: year of type int, month of type int, and day of type int. The class Date should also include the following public member functions:

1.to output the year, month, and day in the format of yyyy-mm-dd.

2.to set the year, month and day according to the parameters. Data validation should be provided. A valid year is an integer between 1900 and 2015 inclusively. A valid month is an integer between 1 and 12 inclusively. A valid day should be an integer between

1 and n, where                n = 31 if month is 1, 3, 5, 7, 8, 10, or 12;                n = 30 if month is 4, 6, 9, or 11;                n = 28 if month = 2.

      Use 1900 for the year, 1 for the month, and 1 for the day, respectively, if a data is invalid.

3.to return the year.

4.to return the month.

5.to return the day.

6.A constructor to initialize year, month, and day with default parameters: The default values of year, month, and day are 1900, 1, and 1 respectively.   

7. to compare two objects’ values. Return true if they are the same, otherwise, return false. has a formal parameter which is a object.

             

You should put the class definition in a header file Date.h and put the class implementation in an implementation file Date.cpp. Write a test program to test your class implementations.

Explanation / Answer


#include<string>
using namespace std;
class Date
{
private:
   int day;
   int month;
   int year;
public:

   Date();
   string displayDate();
   void setYear(int year);
   void setMonth(int month);
   void setDay(int day);
   int getYear();
   int getMonth();
   int getDay();
   bool comp(Date d);

};

------------------------------------

#include"Date.h"
#include<string>
#include<iostream>
using namespace std;

Date::Date()
{
   year = 1900;
   day = 1;
   month = 1;
  
}

void Date::setDay(int d)
{
   if (d < 1)
   {
       d = 1;
       return;
   }
   else
   {
       if (month == 2)
       {
           if (d > 28)
           {
               d = 1;
               return;
           }
       }
       else if (month==1|| month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 )
       {
           if (d > 31)
           {
               d = 1;
               return;
           }

       }
       else
       {
           if (d > 30)
           {
               d = 1;
               return;
           }

       }
      
       day = d;

   }
}

void Date::setMonth(int m)
{
   if (m < 1 || m>12)
   {
       month = 1;
}
   else
   {
       month = m;
   }

}

void Date::setYear(int y)
{
   if (y < 1900 || y>2015)
   {
       year = 1900;
   }
   else
   {
       year = y;
   }
}
int Date::getDay()
{
   return day;
}

int Date::getMonth()
{
   return month;
}


int Date::getYear()
{
   return year;
}

string Date::displayDate()
{
   string s=to_string(year)+"-"+ to_string(month)+"-"+to_string(day);

   return s;
}