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

Given the following class: class dateType { public: void setDate(int month, int

ID: 639710 • Letter: G

Question

Given the following class:

class dateType

{

public:

       void setDate(int month, int day, int year);

       void printDate() const;

       bool isLeapYear();

       dateType(int month = 1, int day = 1, int year = 1900);

private:

       int dMonth;

       int dDay;

       int dYear;

};

Your task is:

Define the function setDate. Make sure that the month is a value between and including 1 and 12 and that the year is a positive number. Also, make sure that the day is a valid date based on the month (for example, the day must be between and including 1 and 31 for the months of Jan (1), March (3),

Explanation / Answer

/*
Class dateType that sets the date
object with day, month and year and
check whether the date is valid or invalid
*/
#ifndef DATE_TYPE
#define DATE_TYPE
#include<iostream>
using namespace std;

//Class definition
class dateType
{
public:
   dateType();
   dateType(int month , int day, int year);
   void setDate(int month, int day, int year);
   void printDate() const;
   bool isLeapYear( );
private:
   int dMonth;
   int dDay;
   int dYear;
   bool checkDay(int,int,int);

};

dateType::dateType()
{
   dMonth=1;
   dDay=1;
   dYear=1900;
}

dateType::dateType(int month,int day,int year)
{
   dMonth=month;
   dDay=day;
   dYear=year;
}

bool dateType::isLeapYear()
{
    if (((dYear % 4 == 0) && (dYear % 100 != 0)) || dYear % 400 == 0)
        return true;
    else
        return false;
}

void dateType::setDate(int month, int day,int year)
{
   if(checkDay(month,day,year))
   {
       dMonth=month;
       dDay=day;
       dYear=year;
   }
   else
   {
       cout<<month<<"-"<<day<<"-"<<
           year<<" Invalid date"<<endl;
   }
}

void dateType::printDate() const
{
   cout<<"Date :"<<dMonth<<"-"<<dDay<<"-"<<dYear<<endl;
}

bool dateType::checkDay(int month,int day,int year)
{
   if(month==1 || month==3 ||month==5 || month==7 ||month==8 || month==10 ||month==12)
   {
       if(day>0 &&day<=31)
           return true;
       else
           return false;
   }
   else if(month==4 || month==6 ||month==9 || month==11)
   {
       if(day>0 &&day<=30)
           return true;
       else
           return false;
   }
   else
   {
       if(month==2 &&isLeapYear())
       {
           if(dDay>0 &&dDay<=29)
               return true;
       }
       else          
           return false;
  
   }
}
#endif DATE_TYPE
------------------------------------------------------------


//Cpp program that creates the objects of dateType
//and calls the printDate and displays whether the
//date is valid or invalid
#include<iostream>
#include "dateType.h"
using namespace std;

int main()
{
   //set default constructor of the dateType
   dateType dateObject;
   //print the date
   dateObject.printDate();


   dateType correctDate;
  
   //set the right date object
   correctDate.setDate(5,2,2015);
   //print the date
   correctDate.printDate();

   //set date with invalid parameters
   dateType invalidDate;
   invalidDate.setDate(2,29,2015);

   system("pause");
}

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

Sample output:

Date :1-1-1900
Date :5-2-2015
2-29-2015 Invalid date

Hope helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote