Design a C++ class to store a date in three integers month day and year
Date
Design a C++ class called Date. The class should store a date in three integers: month, day and year. There should be member functions to print the date in the following forms:
12125109
December 25, 2009
25 December 2009
Demonstrate the class by writing a complete program implemenring it.
input Validation: Do not accept values for the day greater than 31 or less than 1. Do
not accept values for the month greater than 12 or less than 1.
Answer:
// Date Class
#include <iostream>
#include <cstring>
using namespace std;
// Constants
const int NUM_MONTHS = 12;
const int NAME_SIZE = 10;
// Declaration of the Date class
class Date
{
private:
int month; // To hold the month
int day; // To hold the day
int year; // To hold the year
// An array of char arrays to hold
// the names of the months
char names[NUM_MONTHS][NAME_SIZE];
// Private member function to assign
// the month names to the names array
void setNames();
public:
// Constructors
Date();
Date(int, int, int);
// Mutators
void setMonth(int m);
void setDay(int d);
void setYear(int y);
// Functions to print the date
void showDate1();
void showDate2();
void showDate3();
};
//**********************************
// Default constructor *
//**********************************
Date::Date()
{
setNames();
}
//**********************************
// Overloaded constructor *
// Parameters: m is the month *
// d is the day *
// y is the year *
//**********************************
Date::Date(int m, int d, int y)
{
setMonth(m);
setDay(d);
setYear(y);
setNames();
}
//**********************************
// Member function setNames *
// This function assigns the names *
// of the months to the names *
// array. *
//**********************************
void Date::setNames()
{
strcpy(names[0], "January");
strcpy(names[1], "Febraury");
strcpy(names[2], "March");
strcpy(names[3], "April");
strcpy(names[4], "May");
strcpy(names[5], "June");
strcpy(names[6], "July");
strcpy(names[7], "August");
strcpy(names[8], "September");
strcpy(names[9], "October");
strcpy(names[10], "November");
strcpy(names[11], "December");
}
//**********************************
// Member function setMonth *
//**********************************
void Date::setMonth(int m)
{
if (m >= 1 && m <= 12)
month = m;
else
{
cout << m << " is not a valid "
<< "value for the month.\n"; exit (EXIT_FAILURE); } } //********************************** // Member function setDay * //********************************** void Date::setDay(int d) { if (d >= 1 && d <= 31)
day = d;
else
{
cout << d << " is not a valid "
<< "value for the day.\n";
exit(EXIT_FAILURE);
}
}
//**********************************
// Member function setYear *
//**********************************
void Date::setYear(int y)
{
year = y;
}
//**********************************
// Member function showDate1 *
// Displays the date in the form *
// MM/DD/YY *
// Example: 12/25/06 *
//**********************************
void Date::showDate1()
{
cout << month << "/"
<< day << "/"
<< year << endl;
}
//*************************************
// Member function showDate2 *
// Displays the date in the following *
// form: December 25, 2006 *
//*************************************
void Date::showDate2()
{
cout << names[month+1]
<< " " << day << ", "
<< year << endl;
}
//*************************************
// Member function showDate3 *
// Displays the date in the following *
// form: 25 December, 2006 *
//*************************************
void Date::showDate3()
{
cout << day << " "
<< names[month+1]
<< " " << year << endl;
}
//*************************************
// Function main *
//*************************************
int main()
{
// Create a Date object and initialize it
// using the overloaded constructor.
Date today(12, 25, 2006);
// Show the date in form #1.
today.showDate1();
// Store a new month, day, and year
// in the object.
today.setMonth(6);
today.setDay(16);
today.setYear(2006);
// Show the date in form #2.
today.showDate2();
// Show the date in form #3.
today.showDate3();
return 0;
}
Leave a reply