Tags:
create new tag
view all tags

Date class code

Date.h

#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();
    int getMonth();
    int getDay();
    int doy();
private:
    int month;
    int day;
    int year;

};

bool equal(Date a, Date b);

Date.cpp

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

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

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

bool equal(Date a, Date b){
    if (a.getYear()==b.getYear() && a.getMonth()==b.getMonth() && a.getDay()==b.getDay() ) {
        return true;
    }
    return false;
}

DateApp.cpp

#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();
    }
}
Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2018-03-29 - JimSkon
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback