1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
Header file:
//
// Date.h
// Date class with constructor with default arguments
// Member functions defined in Date.cpp
// C++ Programming
// Fall 2014
// Paul XXXXXXXXX
//
//
#ifndef Paul_Date_H
#define Paul_Date_H
#include <string>
using std::cout;
using std::endl;
using std::setfill;
using std::setw;
using std::string;
using namespace std;
class Date
{
public:
// default constructor setting date to January 1, 2000
explicit Date ( int = 1, int = 1, int=2000 );
// set functions
// function to set date
void setDate( int, int, int ); //set date DD/MM/YYYY
// function to set day
void setDay( int ); //set day
// function to set month
void setMonth( int ); // set month
// function to set month name
void setMonthName(string); // set month name
// function to set year
void setYear( int); // set year
// get functions
// function to return the day value of the class Date
int getDay() const; // return day value
// function to return the month value of the class Date
int getMonth() const; //return month value
// function to get month Name
string getMonthName() const; // return the name of the month
// function to return the year value of the class Date
int getYear() const; // return year value
// function to display Date in DD/MM/YYYY format including leading 0 where needed
void displayNumericDate();
// function to display date in long date eg. 1 January, 1900
void displayLongDate();
private:
int month;
int day;
int year;
}; // end of class Date
#endif /* defined(Paul_Date_H) */
__________________________________________________________________
Source code:
// Date.cpp
//
// C++ Programming
// Fall 2014
// Paul XXXXXXXXXX
#include <iostream>
#include <iomanip>
#include <string>
#include <array>
#include "Date.h"
array< const int, 12> numberOfDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
array < const string, 12> monthName = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};
// Date constructor with arguments and validation
Date::Date( int day, int month, int year)
{
setDate( day, month, year ); //validate and set date
} // end Date constructor
// set date value
void Date::setDate( int d, int m, int y)
{
setDay( d ); // get private field day
setMonth( m ); //get private field month
setYear( y ); // get private field year
} // end function setDate
void Date::setDay ( int d)
{
if ( d >= 1 && d <= numberOfDays[ month - 1] )
day = d;
else
day = 1;
} // end of setDay function
void Date::setMonth (int m)
{
month = (m >=1 && m <= 12) ? m : 1;
} // end of setMonth function
void Date::setYear(int y)
{
year = ( y >= 1900 ? y : 1900 );
} // end of setYear function
// return day value
int Date::getDay() const
{
return day;
} // end of getDay function
// return month value
int Date::getMonth() const
{
return month;
} // end of getMonth function
// return the month name
string Date::getMonthName() const
{
return monthName[ Date::getMonth() - 1 ];
}
// return year value
int Date::getYear() const
{
return year;
} // end of getYear function
// print date in DD/MM/YYYY format
void displayNumericDate()
{
cout << setfill( '0' ) << setw( 2 ) << Date::getDay() << "/"
<< setfill( '0') << setw( 2 ) << Date::getMonth()<< "/" << setw( 4 ) << Date::getYear();
} // end of displayNumericDate function
// print date in long date format eg. 1 January, 1900
void displayLongDate()
{
cout << Date::getDay() << " " << Date::getMonthName() << ", " << Date::getYear()
} // end of displayLongDate
|