This is the question: The class dateType was designed to implement the date in a
ID: 3530250 • Letter: T
Question
This is the question:
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.
This is what I have so far. Its a bit confusing. First why does it start with an invalid entry, second I dont get it, is the date suppose to be 0's for the year instead of 1990??? Code compiles successfully but i know something is wrong with this code. Any help, Thanks
#include "stdafx.h"
#include <iostream>
using namespace std;
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.
bool isLeapYear(int);
private:
int dMonth; //variable to store the month
int dDay; //variable to store the day
int dYear; //variable to store the year
};
void dateType::setDate(int month, int day, int year)
{bool error=false;
int max=28;
switch(month)
{ case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12: if (day > 31)
error=true;
break;
case 4 :
case 6 :
case 9 :
case 11: if (day > 30)
error=true;
break;
case 2 : if(isLeapYear(year))
max=29;
if(day>max)
error=true;
}
if(error)
{cout<<"invalid date "<<month<<"/"<<day<<"/"<<year<<" entered. Jan 1, 1900 being used ";
month=1;
day=1;
year=1900;
}
dMonth = month;
dDay = day;
dYear = year;
}
bool dateType::isLeapYear(int year)
{ if((year%4==0&&year0!=0)|| year@0==0)
return true;
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;
}
//Constructor with parameters
dateType::dateType(int month, int day, int year)
{
setDate(month, day, year);
}
int main()
{dateType d1(2,29,2011);
cout<<"First date is ";
d1.printDate();
cout<<endl;
dateType d2(2,29,2008);
cout<<"Second date is ";
d2.printDate();
cout<<endl;
system("pause");
return 0;
}
Explanation / Answer
//changed the code of isLeapYear function working fine with me..
#include <iostream>
using namespace std;
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.
bool isLeapYear(int);
private:
int dMonth; //variable to store the month
int dDay; //variable to store the day
int dYear; //variable to store the year
};
void dateType::setDate(int month, int day, int year)
{bool error=false;
int max=28;
switch(month)
{ case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12: if (day > 31)
error=true;
break;
case 4 :
case 6 :
case 9 :
case 11: if (day > 30)
error=true;
break;
case 2 : if(isLeapYear(year))
max=29;
if(day>max)
error=true;
}
if(error)
{cout<<"invalid date "<<month<<"/"<<day<<"/"<<year<<" entered. Jan 1, 1900 being used ";
month=1;
day=1;
year=1900;
}
dMonth = month;
dDay = day;
dYear = year;
}
bool dateType::isLeapYear(int year)
{ if((year%4==0)&&(year0==0)&&(year@0!=0))
return true;
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;
}
//Constructor with parameters
dateType::dateType(int month, int day, int year)
{
setDate(month, day, year);
}
int main()
{dateType d1(2,29,2011);
cout<<"First date is ";
d1.printDate();
cout<<endl;
dateType d2(2,29,2008);
cout<<"Second date is ";
d2.printDate();
cout<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.