Create Date Class to format date value - C++ Class

C++ examples for Class:Member Function

Description

Create Date Class to format date value

Demo Code

                                                                                                                                                       
#include <string>
                                                                                                                                                       
class Date {/*  ww w  .j  a  va2 s .  c om*/
 public:
    static const int monthsPerYear = 12;
    static const int daysPerWeek = 7;
                                                                                                                                                       
    const int daysPerMonth[monthsPerYear + 1] = {0,  31, 28, 31, 30, 31, 30,
                                                 31, 31, 30, 31, 30, 31};
                                                                                                                                                       
    const std::string monthNames[monthsPerYear + 1] = {
        "",        "January",  "February", "March",  "April",
        "May",     "June",     "July",     "August", "September",
        "October", "November", "December"};
                                                                                                                                                       
    const std::string dayNames[daysPerWeek] = {
        "Sunday",   "Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday"};
                                                                                                                                                       
    Date();
    Date(int, int, int);
    Date(std::string, int, int);
                                                                                                                                                       
    void printDayYear() const;
    void printShort() const;
    void printLong() const;
                                                                                                                                                       
    void setMonth(int);
    void setYear(int);
    void setDay(int);
                                                                                                                                                       
 private:
    int month;
    int day;
    int year;
    std::string weekday;
                                                                                                                                                       
    // utility functions
    int checkDay(int) const;
                                                                                                                                                       
    void setWeekDay();
};
                                                                                                                                                       
#include <ctime>
#include <iostream>
#include <stdexcept>
                                                                                                                                                       
// use system date
Date::Date() {
    std::time_t now = std::time(nullptr);
    struct tm* localTime = localtime(&now);
                                                                                                                                                       
    setMonth(localTime->tm_mon + 1);
    setDay(localTime->tm_mday);
    setYear(1900 + localTime->tm_year);
    setWeekDay();
}
// integer values
Date::Date(int m, int d, int y) {
    setMonth(m);
    setDay(d);
    setYear(y);
    setWeekDay();
}
// string month
Date::Date(std::string m, int d, int y) {
    for (int i = 1; i <= monthsPerYear; ++i) {
        if (monthNames[i] == m) {
            setMonth(i);
            break;
        }
    }
    setDay(d);
    setYear(y);
    setWeekDay();
}
// SETTERS
void Date::setMonth(int m) {
    if (m > 0 && m <= monthsPerYear) {
        month = m;
    } else {
        throw std::invalid_argument("month must be 1-12");
    }
}
void Date::setDay(int d) { day = checkDay(d); }
void Date::setYear(int y) { year = y; }
                                                                                                                                                       
void Date::printDayYear() const { std::cout << weekday << " " << year; }
                                                                                                                                                       
void Date::printShort() const {
    std::cout << month << '/' << day << '/' << year;
}
                                                                                                                                                       
void Date::printLong() const {
    std::cout << monthNames[month] << " " << day << ", " << year;
}
                                                                                                                                                       
int Date::checkDay(int testDay) const {
    if (testDay > 0 && testDay <= daysPerMonth[month]) {
        return testDay;
    }
    // february 29 check for leap year
    if (month == 2 && testDay == 29 &&
        (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))) {
        return testDay;
    }
                                                                                                                                                       
    throw std::invalid_argument("Invalid day for current month and year");
}
void Date::setWeekDay() {
    tm timeStruct = {};
    timeStruct.tm_year = year - 1900;
    timeStruct.tm_mon = month - 1;
    timeStruct.tm_mday = day;
    timeStruct.tm_hour = 12;
                                                                                                                                                       
    std::mktime(&timeStruct);
                                                                                                                                                       
    weekday = dayNames[timeStruct.tm_wday];
}
#include <iostream>
                                                                                                                                                       
int main(int argc, const char *argv[]) {
    Date d1;
    Date d2("February", 14, 1952);
                                                                                                                                                       
    d1.printDayYear();
    printf("\n");
    d1.printShort();
    printf("\n");
    d1.printLong();
    printf("\n\n");
                                                                                                                                                       
    d2.printDayYear();
    printf("\n");
    d2.printShort();
    printf("\n");
    d2.printLong();
    std::cout << std::endl;
                                                                                                                                                       
    return 0;
}

Result


Related Tutorials