Difference: DateClass (1 vs. 2)

Revision 22018-03-29 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="Spring2018"

Date class code

Date.h

%CODE{"c++"}% #include

using namespace std;

class Date { public: Date(); Date(int m, int d, int y); // constructor void input(); void display(); // function to display date int getYear();

Deleted:
<
<
void setYear(int y);
  int getMonth();
Deleted:
<
<
void setMonth(int m);
  int getDay();
Deleted:
<
<
void setDay(int d);
  int doy(); private: int month; int day; int year;

};

bool equal(Date a, Date b); %ENDCODE%

Date.cpp

Changed:
<
<
%CODE{"c++"}%
>
>
%CODE{"c++"}%
 // constructor definition #include "Date.h" static int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; Date::Date () { month = day = 1; year = 0; }

Date::Date (int m, int d, int y) { month = max(1, m); month = min(month,12);

day = max(1,d); day = min(day, days[month]);

year = max(1, y); }

void Date::display() { static string name[] = {"nothing", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

cout << '\n' << name[month] << ' ' << day << "," << year << '\n'; cout << "Days so far: " << doy() << '\n'; }

void Date::input() { cout << "Enter date in form month day year:"; cin >> month >> day >> year; month = max(1, month); month = min(month,12);

day = max(1,day); day = min(day, days[month]); }

int Date::doy() { int total = 0;

for (int i=1; i < month; i++) total += days[i]; total += day; return (total); }

int Date::getMonth() { return month; }

Deleted:
<
<
void Date::setMonth(int n) { month = max(1, n); month = min(month, 12); }
 int Date::getDay() { return day; }
Deleted:
<
<
void Date::setDay(int d) { day = max(1, d); day = min(day, days[month]); }
 int Date::getYear() { return year; }
Deleted:
<
<
void Date::setYear(int y) { year = max(0, y); }
 bool equal(Date a, Date b){ if (a.getYear()==b.getYear() && a.getMonth()==b.getMonth() && a.getDay()==b.getDay() ) { return true; } return false; } %ENDCODE%

DateApp.cpp

%CODE{"c++"}% #include #include "Date.h"

using namespace std;

int main() { Date a, b; Date c(4, 11, 2018);

a.display(); b.display(); c.display();

a.input(); b.input();

a.display(); b.display(); c.display();

if (equal(a, b)) { a.display(); cout << "Is equal to" << endl; b.display(); }

Changed:
<
<
} %ENDCODE%
>
>
}%ENDCODE%
 \ No newline at end of file
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback