Date.h
#include <string>
#ifndef DATE_H_
#define DATE_H_
class Date
{
public:
static const unsigned int monthsPerYear{12};
explicit Date(unsigned int d = 1, unsigned int m = 1, unsigned int y = 1900);
std::string toString() const;
private:
unsigned int day;
unsigned int month;
unsigned int year;
unsigned int checkDay(unsigned int ) const;
};
#endif /* DATE_H_ */
Date.cpp
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <array>
#include "Date.h"
using namespace std;
Date::Date(unsigned int d, unsigned int m, unsigned int y) : day{checkDay(d)}, month{m}, year{y} {
if ( month < 1 || month > monthsPerYear ) {
throw invalid_argument("wrong entry for month");
}
}
string Date :: toString() const {
ostringstream output;
output << day << ":" << month << "/" << year;
return output.str();
}
unsigned int Date :: checkDay(unsigned int testDay) const {
static const array<unsigned, monthsPerYear + 1> daysPerMonth{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (testDay <= daysPerMonth[month] && testDay > 0) {
return testDay;
}
return testDay;
}
main.cpp
#include <iostream>
#include "Date.h"
using namespace std;
int main()
{
Date d2;
cout << d2.toString() << endl;
return 0;
}
i get nothing in the output console.