-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.cpp
More file actions
42 lines (33 loc) · 1.14 KB
/
Date.cpp
File metadata and controls
42 lines (33 loc) · 1.14 KB
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
#include <array>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include "Date.h"
using namespace std;
Date::Date(unsigned int mn, unsigned int dy, unsigned int yr)
: month{mn}, day{checkDay(dy)}, year{yr} {
if (mn < 1 || mn > monthsPerYear) { // Corrected variable name
throw invalid_argument("month must be 1-12");
}
cout << "Date object constructor for date " << toString() << endl;
}
string Date::toString() const {
ostringstream output;
output << month << '/' << day << '/' << year;
return output.str();
}
Date::~Date() {
cout << "Date object destructor for date " << toString() << endl;
}
unsigned int Date::checkDay(int testDay) const {
static const array<int, monthsPerYear + 1> daysPerMonth{
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (testDay > 0 && testDay <= daysPerMonth[month]) {
return testDay;
}
if (month == 2 && testDay == 29 && (year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0))) {
return testDay;
}
throw invalid_argument("Invalid day for the current month and year");
}