I am working on a textbook question in C++ Programming: From Problem Analysis to
ID: 3622915 • Letter: I
Question
I am working on a textbook question in C++ Programming: From Problem Analysis to Program Design (4th) By Malik, Chapter 13 #6. I am trying to work on the header files, but could use another set of hands to finish this.#ifndef date_H
#define date_H
class dateType
{
public:
void setDate(int month, int day, int year);
//Function to set the date.
//Data members dMonth, dDay, and dYear are set
//according to the parameters
//Post: dMonth = month; dDay = day;
// dYear = year;
void getDate(int& month, int& day, int& year);
//Function to return the date
//Post: month = dMonth; day = dDay;
// year = dYear;
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
//Data members dMonth, dDay, and dYear are set
//according to the parameters. If no values are
//specified when an object is declares, the default
//values are used.
//Post: dMonth = month; dDay = day;
// dYear = year;
private:
bool isLeapYear(int y);
//Function to check if a year is a leap year
int dMonth; //variable to store month
int dDay; //variable to store day
int dYear; //variable to store year
};
#endif
Explanation / Answer
NOTE: I used date.h you need to change that to your file name
#include "date.h"
#include <iostream>
using namespace std;
void dateType::setDate(int month, int day, int year)
{
if(month<0 || month>12)
return;
if(month==9 || month==4 || month==6 || month==11)
{
if(day>30)
return;
}
else if (month!=2)
{
if(day>31)
return;
}
else
{
if(year%4==0)
{
if(day>29)
return;
}
else
{
if(day>28)
return;
}
}
if(day<1)
return;
if(year<1)
return;
//set values
dYear=year;
dDay=day;
dMonth=month;
}
void dateType::getDate(int& month, int& day, int& year)
{
month=dMonth;
day=dDay;
year=dYear;
}
void dateType::printDate() const
{
if(dMonth<10)//month is 1 digit
cout<<'0';
cout<<dMonth<<"-";
if(dDay<10)//day is 1 digit
cout<<'0';
cout<<dDay<<"-";
if(dYear<10)//year is 1 digit
cout<<'0';
if(dYear<100)//year is 2 digits or less
cout<<'0';
if(dYear<1000)//year is 3 digits or less
cout<<'0';
cout<<dYear<<' ';
}
dateType::dateType(int month, int day, int year)
{
//check values
if(month<0 || month>12)
month=1;
if(month==9 || month==4 || month==6 || month==11)
{
if(day>30)
day=1;
}
else if (month!=2)
{
if(day>31)
day=1;
}
else
{
if(year%4==0)
{
if(day>29)
day=1;
}
else
{
if(day>28)
day=1;
}
}
if(day<1)
day=1;
if(year<1)
year=1990;
//set values
dYear=year;
dDay=day;
dMonth=month;
}
bool dateType::isLeapYear(int y)
{
return y%4==0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.