Please write the code for this question using C++: I need help with my programmi
ID: 3546629 • Letter: P
Question
Please write the code for this question using C++:
I need help with my programming question. I have most of the code, but something isn't right. I need help on figuring it out.
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.
Here is the code that I have so far:
#include<iostream>
using namespace std;
//Class declaration
class dateType
{
private:
int dMonth;
int dDay;
int dYear;
public:
//Member function declaration
void setDate (int month, int day, int year);
int getDay () const;
int getMonth () const;
int getYear () const;
void printDate () const;
bool isLeapYear (int year);
dateType (int month=0, int day=0, int year=0);//Constructor
};
//Member function definitions
void dateType:: setDate (int month, int day, int year)
{
int noDays;
if (year<=2013)
{//condition to check whether year is valid
dYear=year;
if (month<=12)
{
//condition for each month
dMonth=month;
switch(month)
{
//for number of days in each month
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: noDays=31;
break;
case 4:
case 6:
case 9:
case 11: noDays=31;
break;
case 2: if (isLeapYear (year))
noDays=29;
else
noDays=28;
}
if (day<=noDays)
{//condition for days based on days in month
dDay=day;
}
else
{
cout<<"Invalid Day"<<endl;
dDay=0;
}
}
else
{
cout<<"Invalid Month"<<endl;
dMonth=0;
}
}
else
{
cout<<"Invalid Year"<<endl;
dYear=0;
}
}//End setDate
bool dateType::isLeapYear (int year)
{
//Function to conform whether year is leap year or not
if(year%4==0)
return true;
else
return false;
}
void dateType::printDate ()const
{
cout<<dMonth<<"-"<<dDay<<"-"<<dYear;
}
int dateType::getMonth ()const
{
return dMonth;
}
int dateType::getDay ()const
{
return dDay;
}
int dateType::getDay ()const
{
return dYear;
}
//Constructor
dateType::dateType (int month, int day, int year)
{
dMonth=month;
dDay=day;
dYear=year;
}
void main()
{
int m,d,y;
dateType date (0,0,0);//contructor call
//Inputing values
cout<<"Enter Month";
cin>>m;
cout<<"Enter Day";
cin>>d;
cout<<"Enter year";
cin>>y;
date.setDate (m,d,y)
bool check =date.isLeapYear (y);
if(check)
cout<<"Leap year:"
date.printDate ();
system ("pause");
}//End main
?
int dateType
?
Explanation / Answer
#include <cstdlib>
#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)&&(year %100==0)||(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;
}
//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;
}