class dateType Download source and header files of the class dateType from Black
ID: 3810431 • Letter: C
Question
class dateType Download source and header files of the class dateType from Blackboard. 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 vari- ables. If m, d and y denote day number, month number and year number, then If y s 0, then yis an invalid yearnumber. Change it to the default value of y- 1900 If m s 0 or me 2 13, then m is an invalid month number. Change it to the default value of m 1 If m s 0 or m 213, then m is an invalid month number. Change it to the default value of m>1 If m E {1,3,5,7,8, 10, 12): If d s 0 or d 2 32, then d is an invalid day number. Change it to the default value of d 1 If m E 14, 6, 9, 11 If d s 0 or d 2 31, then d is an invalid day number. Change it to the default value of d 1 If m 2: If y is a leap year number: If d s 0 or d 2 30, then d is an invalid day number. Change it to the default value of d 1Explanation / Answer
//dateType.h
#ifndef dateType_H
#define dateType_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.
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
bool isLeapYear(int year);
};
#endif
-------------------------- -------------------------- --------------------------
//dateType.cpp
//Implementation file date
#include <iostream>
#include "dateType.h"
using namespace std;
dateType::dateType(int month , int day, int year )
{
setDate(month,day,year);
}
//setDate method that tests the month,day and year chekcing
void dateType::setDate(int month, int day, int year)
{
if(year<=0)
dYear=1900;
else
dYear = year;
if(month<=0 || month>=13)
dMonth=1;
else
dMonth = month;
if(dMonth==1 ||dMonth==3 ||dMonth==5 ||dMonth==7 ||
dMonth==8 ||dMonth==10 ||dMonth==12)
{
if(day<=0 || day>=32)
dDay = 1;
else
dDay = day;
}
else if(dMonth==4 ||dMonth==6 ||dMonth==9 ||dMonth==11 )
{
if(day<=0 || day>=31)
dDay = 1;
else
dDay = day;
}
else
{
if(isLeapYear(dYear) && ( day<=0 ||day>=30))
dDay = 1;
else
dDay = day;
}
}
//Method :isLeapYear(int year)
//Returns boolean value if year is leap year otherwise false
bool dateType::isLeapYear(int year)
{
if (year % 4 != 0)
return false;
if (year % 100 != 0)
return true;
if (year % 400 == 0)
return true;
else
return false;
}
int dateType::getDay() const
{
return dDay;
}
int dateType::getMonth() const
{
return dMonth;
}
int dateType::getYear() const
{
return dYear;
}
void dateType::printDate() const
{
cout << dMonth << "-" << dDay << "-" << dYear;
}
-------------------------- -------------------------- --------------------------
//Test c++ program
//header files
#include<iostream>
//include dateType.h header file
#include "dateType.h"
using namespace std;
int main()
{
//default construcotr
dateType d1;
d1.printDate();
cout<<endl;
//parameter construcotr
dateType d2(4,4,2017);
d2.printDate();
cout<<endl;
//incorrect month date
dateType d3(13,1,2017);
d3.printDate();
cout<<endl;
//incorrect day date
dateType d4(12,32,2017);
d4.printDate();
cout<<endl;
//incorrect month date
dateType d5(2,30,2004);
d5.printDate();
cout<<endl;
//pause program output
system("pause");
return 0;
}
-------------------------- -------------------------- --------------------------
Sample Ouptut:
1-1-1900
4-4-2017
1-1-2017
12-1-2017
2-1-2004
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.