date.cpp如下:
**********************************************************************
#include <iostream>
#include "date.h"
date::date () : day(0), month(0), year(0) {}
date::date (int day, int month, int year) :
day(day), month(month), year(year) {}
int date::compareTo (date another_date) {
if (year != another_date.year)
return year - another_date.year;
if (month != another_date.month)
return month - another_date.month;
if (day != another_date.day)
return day - another_date.day;
return 0;
}
ostream &operator<< (ostream &stream, date d) {
stream << d.month << '/' << d.day << '/' << d.year;
return stream;
}
***********************************************************************
homework.cpp如下:
***********************************************************************
#include <iostream>
#include <string>
#include "homework.h"
homework::homework () : name(""), assigned_date(), due_date() {}
homework::homework (string name, date assigned_date, date due_date) :
name(name), assigned_date(assigned_date), due_date(due_date) {}
int homework::compareTo (homework another_homework) {
return (this->due_date.compareTo(another_homework.due_date));
}
ostream &operator<< (ostream &stream, homework h) {
stream << h.name << ": " << h.assigned_date << '-' << h.due_date;
return stream;
}
***********************************************************************
homeworklist.cpp如下:
***********************************************************************
#include "homeworklist.h"
homeworklist::homeworklist() : current_size(0) {}
bool homeworklist::add (homework h) {
if (current_size == LIST_MAX)
return false;
list[current_size++] = h;
return true;
}
homeworklist homeworklist::dueafter (date d) {
homeworklist new_list;
homework reference_date ("reference assignment", d, d);
for (int index = 0; index < current_size; index++) {
if (list[index].compareTo (reference_date) > 0) {
new_list.add (list[index]);
}
}
return new_list;
}
homeworklist homeworklist::duebefore (date d) {
homeworklist new_list;
homework reference_date ("reference assignment", d, d);
for (int index = 0; index < current_size; index++) {
if (list[index].compareTo (reference_date) < 0) {
new_list.add (list[index]);
}
}
return new_list;
}
homeworklist homeworklist::dueon (date d) {
homeworklist new_list;
homework reference_date ("reference assignment", d, d);
for (int index = 0; index < current_size; index++) {
if (list[index].compareTo (reference_date) == 0) {
new_list.add (list[index]);
}
}
return new_list;
}
ostream &operator<< (ostream &stream, homeworklist hl) {
for (int index = 0; index < hl.current_size; index++) {
stream << hl.list[index] << endl;
}
return stream;
}