(Date Class) Create a class called Date that includes three pieces of information as data members—a month (type int), a day (type int) and a year (type int). Your class should have a con- structor with three parameters that uses the parameters to initialize the three data members. For the purpose of this exercise, assume that the values provided for the year and day are correct, but ensure
that the month value is in the range 1–12; if it isn’t, set the month to 1. Provide a set and a get func- tion for each data member. Provide a member function displayDate that displays the month, day and year separated by forward slashes (/). Write a test program that demonstrates class Date’s capa- bilities.
What will be an ideal response?
```
// Definition of class Date.
// class Date definition
class Date
{
public:
Date( int, int, int ); // constructor initializes data members
void setMonth( int ); // set month
int getMonth(); // return month
void setDay( int ); // set day
int getDay(); // return day
void setYear( int ); // set year
int getYear(); // return year
void displayDate(); // displays date in mm/dd/yyyy format
private:
int month; // the month of the date
int day; // the day of the date
int year; // the year of the date
}; // end class Date
```
```
// Member-function definitions for class Date.
#include
#include "Date.h" // include definition of class Date from Date.h
using namespace std;
// Date constructor that initializes the three data members;
// assume values provided are correct (really should validate)
Date::Date( int m, int d, int y )
{
setMonth( m );
setDay( d );
setYear( y );
} // end Date constructor
// set month
void Date::setMonth( int m )
{
month = m;
if ( month < 1 )
month = 1;
if ( month > 12 )
month = 1;
} // end function setMonth
// return month
int Date::getMonth()
{
return month;
} // end function getMonth
// set day
void Date::setDay( int d )
{
day = d;
} // end function setDay
// return day
int Date::getDay() {
return day;
} // end function getDay
// set year
void Date::setYear( int y )
{
year = y;
} // end function setYear
// return year
int Date::getYear()
{
return year;
} // end function getYear
// print Date in the format mm/dd/yyyy
void Date::displayDate()
{
cout << month << '/' << day << '/' << year << endl;
} // end function displayDate
```
```
// Demonstrates class Date's capabilities.
#include
#include "Date.h" // include definition of class Date from Date.h
using namespace std;
// function main begins program execution
int main()
{
Date date( 5, 6, 1981 ); // create a Date object for May 6, 1981
// display the values of the three Date data members
cout << "Month: " << date.getMonth() << endl;
cout << "Day: " << date.getDay() << endl;
cout << "Year: " << date.getYear() << endl;
cout << "\nOriginal date:" << endl;
date.displayDate(); // output the Date as 5/6/1981
// modify the Date
date.setMonth( 13 ); // invalid month
date.setDay( 1 );
date.setYear( 2005 );
cout << "\nNew date:" << endl;
date.displayDate(); // output the modified date (1/1/2005)
return 0; // indicate successful termination
} // end main
```
Month: 5
Day: 6
Year: 1981
Original date:
5/6/1981
New date:
1/1/2005
You might also like to view...
Find out the domain name of the web server host of your organization. What is its IP address?
What will be an ideal response?
Excel creates charts on the same worksheet as the original data set, but you can move the chart to its own chart sheet in the workbook
Indicate whether the statement is true or false
What is the meaning of the following mnemonics (and what do they do)?
a. LDRSH b RSBLES c CMPS d MRS r0,CPSR
Eachincludeandimportelement in a schema must be globally declared as a direct child of the _____schemaelement.
A. host B. root C. base D. tree