In: Computer Science
Programming Exercise 11-2
QUESTION: In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check whether a year is a leap year. Moreover, write a test program to test your class.
Input should be format month day year with each separated by a space. Output should resemble the following:
Date #: month-day-year
An example of the program is shown below:
Date 1: 3-15-2008 this is a leap year
If the year is a leap year, print the date and a message indicating it is a leap year, otherwise print a message indicating that it is not a leap year.
The header file for the class dateType has been provided for you.
GIVEN FILE: dateType.h
FILES NEEDED: dateTypeImp.cpp, main.cpp
~~~~~~dateType.h~~~~~~
#ifndef date_H
#define date_H
class dateType
{
public:
void setDate(int month, int day, int year);
//Function to set the date.
//The member variables dMonth, dDay, and dYear are set
//according to the parameters
//Postcondition: dMonth = month; dDay = day;
// dYear = year
int getDay() const;
//Function to return the day.
//Postcondition: The value of dDay is returned.
int getMonth() const;
//Function to return the month.
//Postcondition: The value of dMonth is returned.
int getYear() const;
//Function to return the year.
//Postcondition: The value of dYear is returned.
void printDate() const;
//Function to output the date in the form mm-dd-yyyy.
bool isLeapYear();
//Function to determine whether the year is a leap year.
dateType(int month = 1, int day = 1, int year = 1900);
//Constructor to set the date
//The member variables dMonth, dDay, and dYear are set
//according to the parameters
//Postcondition: dMonth = month; dDay = day;
// dYear = year
//If no values are specified, the default values are
//used to initialize the member variables.
private:
int dMonth; //variable to store the month
int dDay; //variable to store the day
int dYear; //variable to store the year
};
#endif
Comment down
for any queries
Please give a thumbs up if you are satisfied with the
answer
//FIRST HEADER FILE OF dateType.h
#include<string>
#include<iostream>
using namespace std;
class dateType
{
private:
int dMonth; //variable to store the month
int dDay; //variable to store the day
int dYear; //variable to store the year
public:
dateType();
dateType(int m, int d, int y);
void setDate(int
month, int day, int year);
//Function to return the day.
//Postcondition: The value of
dDay is returned.
int getDay() const;
//Function to return the month.
//Postcondition: The value of dMonth is returned.
int getMonth() const;
//Function to return the year.
//Postcondition: The value of dYear is returned.
int getYear() const;
//Function to output the date in
the form mm-dd-yyyy.
void printDate();
//Function to determine whether
the year is a leap year.
bool isLeapYear();
};
//dateType.cpp FIRST CPP FILE
#include "DataType.h"
dateType::dateType()
{
dMonth = 1;
dDay = -1;
dYear = 1900;
}
dateType::dateType(int m, int d , int y )
{
if (m > 0 && m <= 12)
{
dMonth = m;
}
else
{
cout << "Month entered is
wrong" << endl;
dMonth = 1;
}
if (d > 0 && d <= 31)
{
dDay = d;
}
else
{
cout << "Day entered is
wrong" << endl;
dDay = 1;
}
dYear = y;
}
//Function to set the date.
//The member variables dMonth, dDay, and dYear are set
//according to the parameters
//Postcondition: dMonth = month; dDay = day;
// dYear = year
void dateType::setDate(int month, int day, int year)
{
if (month > 0 && month <= 12)
{
dMonth = month;
}
if (day > 0 && day <= 31)
{
dDay = day;
}
dYear = year;
}
//Function to return the day.
//Postcondition: The value of dDay is returned.
int dateType::getDay() const
{
return dDay;
}
//Function to return the month.
//Postcondition: The value of dMonth is returned.
int dateType::getMonth() const
{
return dMonth;
}
//Function to return the year.
//Postcondition: The value of dYear is returned.
int dateType::getYear() const
{
return dYear;
}
//Function to output the date in the form mm-dd-yyyy.
void dateType::printDate()
{
cout << "Month-Day-Year" << endl;
if (isLeapYear() == true)
{
cout << "It's a Leap Year"
<< endl;
}
else
{
cout << "Not a leap year"
<< endl;
}
cout << dMonth << "-" << dDay
<< "-" << dYear << endl;
}
//Function to determine whether the year is a leap year.
bool dateType::isLeapYear()
{
cout << "Year is:" << dYear << endl;
if (dYear % 4 == 0)
{
if (dYear % 100 == 0)
{
if (dYear % 400
== 0)
return true;
else
return false;
}
else
return
true;
}
else
return false;
}
//2nd CPP FILE MAINCPP
#include"DataType.h"
#include<string>
#include<cmath>
using namespace std;
int main()
{
int day, month, year;
cout << "Month:";
cin >> month;
cout << "Day:";
cin >> day;
cout << "Year:";
cin >> year;
dateType obj(month, day, year);
obj.printDate();
}
Comment down for any
queries
Please give a thumbs up if you are satisfied with the
answer