Starting with obj2.C,

The distance friend of class date

  1. kheslin2/three.C declared the distance function but forgot to define it. Ditto for the constructor with no explicit arguments. Two variables in main with the same name (t).

  2. ak205/three.C thinks that the distance from December 31, 2024 to January 1, 2025 is 366 days. It should be 1 day. We will handle leap year later in the course when we do inheritance and polymorphism. The code to test if the year i is a leap year can be simplified from
    	if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)) {
    
    to
    	if (i % 400 == 0 || i % 100 != 0 && i % 4 == 0) {
    
    but this doesn’t fix the bug.

  3. jr224/three.C thinks that the distance from January 1, 2025 to January 1, 2026 is 0 days. It should be 365 days.

Three ways to define date::dayOfYear

For example, today (February 13, 2025) is day number 31 + 13 = 44 of the year.

// Return what day of the year (1 to 365 inclusive) this date object is.

int date::dayOfYear() const
{
	int sum {0};

	for (int i {1}; i < month; ++i) {
		sum += length[i];
	}

	sum += day;
	return sum;
}
// Return what day of the year (1 to 365 inclusive) this date object is.

int date::dayOfYear() const
{
	int sum {day};

	for (int i {1}; i < month; ++i) {
		sum += length[i];
	}

	return sum;
}

Remember,
length means &length[0]
length + 1 means &length[1]
length + month means &length[month]

This call to the accumulate function returns an int is because it’s looping through an array of ints.

#include <algorithm>   //for the accumulate function
using namespace std;

// Return what day of the year (1 to 365 inclusive) this date object is.

int date::dayOfYear() const
{
	return accumulate(length + 1, length + month, day);
}