Difference: DateClass (1 vs. 2)

Revision 22018-03-29 - JimSkon

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

Date class code

Date.h

Line: 15 to 15
  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;
Line: 32 to 29
 %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 };
Line: 88 to 84
  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;
Line: 151 to 130
  cout << "Is equal to" << endl; b.display(); }
Changed:
<
<
} %ENDCODE%
>
>
}%ENDCODE%
 \ No newline at end of file

Revision 12018-03-27 - JimSkon

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

Date class code

Date.h

<-- SyntaxHighlightingPlugin -->
#include <iostream>

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();
    void setYear(int y);
    int getMonth();
    void setMonth(int m);
    int getDay();
    void setDay(int d);
    int doy();
private:
    int month;
    int day;
    int year;

};

bool equal(Date a, Date b);
<-- end SyntaxHighlightingPlugin -->

Date.cpp

<-- SyntaxHighlightingPlugin -->
// 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;
}

void Date::setMonth(int n)
{
   month = max(1, n);
   month = min(month, 12);
}

int Date::getDay()
{
   return day;
}

void Date::setDay(int d)
{
   day = max(1, d);
   day = min(day, days[month]);
}

int Date::getYear()
{
   return year;
}

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;
}
<-- end SyntaxHighlightingPlugin -->

DateApp.cpp

<-- SyntaxHighlightingPlugin -->
#include <iostream>
#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();
    }
}
<-- end SyntaxHighlightingPlugin -->
 
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