Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "myDate.h"
- #include <iostream>
- #include <iomanip>
- using namespace std;
- //Constructor
- //Description Sets month to 1, day to 1, year to 2015
- //Parameters: none
- myDate::myDate()
- {
- month=1;
- day =1;
- year=2015;
- }
- myDate::myDate(int dy, int yr, int mnth)
- {
- month=mnth;
- day=dy;
- year=yr;
- }
- //gets and sets
- //sets month,day,year,time
- void myDate::setMonth(int mnth) {month=mnth;}
- void myDate::setDay(int dy) {day=dy;}
- void myDate::setYear(int yr) {year=yr;}
- void myDate::setTime (const myTime &tm) {theTime=tm;}
- //gets month,day,year,time
- int myDate::getMonth()const {return month;}
- int myDate::getDay()const {return day;}
- int myDate::getYear()const {return year;}
- myTime myDate::getTime()const {return theTime;}
- //relation operators: compare full date and time
- //A later date/time is "greater than an earlier one
- bool myDate::operator==(const myDate &date) const
- {
- return (year == date.getYear() && month==date.getMonth() && day==date.getDay() && theTime==date.getTime());
- }
- bool myDate::operator!=(const myDate &date) const
- {
- return (year != date.getYear() || month!=date.getMonth() || day!=date.getDay() || theTime!=date.getTime());
- }
- bool myDate::operator>(const myDate &date)const
- {
- if (year > date.getYear())
- return true;
- else if (year<date.getYear())
- return false;
- else if (month>date.getMonth())
- return true;
- else if (month<date.getMonth())
- return false;
- else if (day>date.getDay())
- return true;
- else if (day<date.getDay())
- return false;
- else if (theTime>date.dateTime())
- return true;
- else
- return false;
- }
- bool myDate::operator<(const myDate &date) const
- {
- if (year>date.getYear())
- return false;
- else if (year<date.getYear())
- return true;
- else if (month>date.getMonth())
- return false;
- else if (month<date.getMonth())
- return true;
- else if (day>date.getDay())
- return false;
- else if (day<date.getDay())
- return true;
- else if (theTime>date.getTime())
- return false;
- else
- return true;
- }
- bool myDate::operator>=(const myDate &date) const
- {
- if (*this>date ||*this ==date)
- return true;
- else
- return false;
- }
- bool myDate::operator<=(const myDate &date)const
- {
- if(*this<date ||*this==date)
- return true;
- else
- return false;
- }
- //Name: compareDates
- int myDate::compareDates(const myDate &secondDate) const
- {
- if(*this>secondDate)
- {
- return -1;
- }
- else if(*this==secondDate)
- {
- return 0;
- }
- else
- return 1;
- }
- //Adds days to the date
- myDate &myDate::operator+=(int dy)
- {
- day+=dy;
- return *this;
- }
- string myDate::toLongDate()
- {
- string wordDate;
- if(month==1)
- {
- wordDate="January";
- }
- else if (month==2)
- {
- wordDate="February";
- }
- else if(month==3)
- {
- wordDate="March";
- }
- else if (month==4)
- {
- wordDate="April";
- }
- else if (month==5)
- {
- wordDate="May";
- }
- else if (month==6)
- {
- wordDate="June";
- }
- else if (month==7)
- {
- wordDate="July";
- }
- else if (month==8)
- {
- wordDate="August";
- }
- else if (month==9)
- {
- wordDate="September";
- }
- else if (month==10)
- {
- wordDate="October";
- }
- else if (month==11)
- {
- wordDate="November";
- }
- else
- wordDate="December";
- return wordDate;
- }
- bool myDate::isLeapYear()const
- {
- if ((year%4)!=0)
- {
- return false;
- }
- else if((year%4)==0 && (year%100)!=0)
- {
- return true;
- }
- else if((year%4)==0 &&(year%100)==0)
- {
- if((year%400)==0)
- {
- return true;
- }
- else
- return false;
- }
- else ;
- }
- int myDate::daysInMonth()const
- {
- int daysinmonth;
- if(month==1)
- {
- daysinmonth=31;
- }
- else if (month==2)
- {
- if(isLeapYear()==true)
- {
- daysinmonth=29;
- }
- else
- daysinmonth=28;
- }
- else if (month==3)
- {
- daysinmonth=31;
- }
- else if (month==4)
- {
- daysinmonth=30;
- }
- else if (month==5)
- {
- daysinmonth=31;
- }
- else if (month==6)
- {
- daysinmonth=30;
- }
- else if (month==7)
- {
- daysinmonth=31;
- }
- else if (month==8)
- {
- daysinmonth=31;
- }
- else if (month==9)
- {
- daysinmonth=30;
- }
- else if (month==10)
- {
- daysinmonth=31;
- }
- else if (month==11)
- {
- daysinmonth=30;
- }
- else
- {
- daysinmonth=31;
- }
- }
- ostream &operator<<(ostream &output, const myDate &date)
- {
- output<< date.getMonth() <<"/"<<date.getDay()<<"/"<<date.getYear();
- return output;
- }
- istream &operator>>(istream &input, myDate &date)
- {
- int dy, mnth,yr;
- char slash;
- input>>mnth>>slash>>dy>>slash>>yr;
- date.setMonth(mnth);
- date.setDay(dy);
- date.setYear(yr);
- return input;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement