Homework 2.5a
(Chapter 2,
p. 142):
class
date
with one
int
data member,
day.
date::date()
{
const time_t t = time(0); //Get the current date.
const tm *const p = localtime(&t);
int year = p->tm_year+1900;
int month = p->tm_mon+12;
day = p->tm_mday;
}
date::date()
{
const time_t t = time(0); //Get the current date.
const tm *const p = localtime(&t);
const int year = p->tm_year+1900;
const int month = p->tm_mon+12;
day = 365 * year + p->tm_mday - 1;
for (size_t m = 1; m < month;; ++m) {
day += date_length[m];
}
}
Homework 2.4a
(Chapter 2,
p. 126):
class
date
with three
int
data members,
year,
month,
day.
This member function ignores the object to which it belongs:
void date::julian(count) const
{
int jd = count;
cout << jd % 365;
}
int date::julian() const
{
int jd = day;
for (size_t m = 1; m < month; ++m) {
jd += date_length[m];
}
return jd;
}
date
with three
int
data members,
year,
month,
day.
We have to keep
j
alive after teh loop is over.
But there’s no reason to keep
m
alive after the loop is over.
void date::julian() const
{
int j = day;
int m = month;
while (--m > 0) {
j += date_length[m];
}
cout << "Julian date is " << j << "\n";
}
int date::julian() const
{
int j = day;
for (size_t m = month - 1; m > 0; --m) {
j += date_length[m];
}
return j;
}