/* Date.cpp Project: NamespaceDate Demonstrate how to create and use a namespace Based on the Date class used to demonstrate separate compilation. Most comments are removed so that we can focus on the namespace issues. */ #include "Date.h" #include #include // atoi. Note we also get it from string. using namespace std; // In the implementation file it is good to wrap all the definitions // in a "namespace ASillyName { ... }" // just as we did in the header file. // [ While there are other approaches that would work in this case, // this will prove to be the consistently safe approach. ] namespace ASillyName { Date::Date(const string& date) { month = atoi(date.substr(0,2).c_str()); day = atoi(date.substr(3,2).c_str()); year = atoi(date.substr(6,4).c_str()); } void Date::display(std::ostream& os) const { os << month << '/' << day << '/' << year; } bool Date::earlierThan(const Date& d) const { if (year > d.year) return false; else if (year < d.year) return true; else if (month > d.month) return false; else if (month < d.month) return true; else if (day >= d.day) return false; else return true; } int Date::getYear() const { return year; } void Date::setYear(int x) { year = x; } }