#include #include // For time() and localtime() using namespace std; class date { public: // Data members to store month, day, and year. int month; int day; int year; // Static array to hold the number of days in each month (non-leap year). // Note: Index 0 is not used; months are 1-indexed. static const int length[13]; // Constructor with three explicit arguments. date(int m, int d, int y) : month(m), day(d), year(y) { } // No-argument constructor that sets the date to today's date. date() { // Get current time. time_t now = time(nullptr); // Convert to local time structure. tm *lt = localtime(&now); // tm_mon is 0-indexed so add 1; tm_mday and tm_year (offset by 1900) give the day and year. month = lt->tm_mon + 1; day = lt->tm_mday; year = lt->tm_year + 1900; } // Function to print the date in MM/DD/YYYY format. void print() const { cout << month << "/" << day << "/" << year; } // Member function that calculates the day of the year (1 to 365). int dayOfYear() const { int sum = 0; // Sum the days in all months preceding the current month. for (int m = 1; m < month; ++m) { sum += length[m]; } // Add the current day. sum += day; return sum; } // Friend function declaration to compute the distance in days between two dates. friend int distance(const date& d1, const date& d2); }; // Define the static array for month lengths. // For non-leap years: index 1 = January, 2 = February, ..., 12 = December. const int date::length[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // Friend function definition. // Returns the difference in days: positive if d1 is later than d2, zero if the same, negative otherwise. // For simplicity, if the dates are in different years, we adjust by assuming 365 days per year. int distance(const date& d1, const date& d2) { if (d1.year == d2.year) { return d1.dayOfYear() - d2.dayOfYear(); } else { // Calculate a rough difference by converting the year to days (this doesn't account for leap years) int days1 = d1.dayOfYear() + d1.year * 365; int days2 = d2.dayOfYear() + d2.year * 365; return days1 - days2; } } int main() { // Test the no-argument constructor: it should initialize to today's date. const date t; // Calls the new no-arg constructor. cout << "Today is "; t.print(); // Prints today's date. cout << ".\n"; // Test the dayOfYear member function. const date xmas {12, 25, 2025}; // Christmas 2025. cout << "Christmas is day number " << xmas.dayOfYear() << " of the year.\n"; // Test the friend function distance. const date summer {6, 20, 2025}; // First day of summer. const int dist {distance(summer, t)}; cout << "Only " << dist << " days till summer.\n"; return 0; }