You are on page 1of 6

#include<cstring> #include<iostream> using namespace std; //header file #ifndef MYDATE_H #define MYDATE_H class MyDate { private: short

int day,mon,year; public: MyDate(short int =1,short int =1,short int =2011); MyDate(const MyDate &); MyDate operator=(const MyDate &); bool operator==(const MyDate &); bool operator>(const MyDate &); bool operator<(const MyDate &); bool operator>=(const MyDate &); bool operator<=(const MyDate &); void dispdate(); MyDate operator++(); MyDate operator--(); MyDate operator++(int =1); MyDate operator--(int =1); ~MyDate(); }; bool leap(short int year); bool checkvalid(short int,short int,short int); short int get_month(char mm[4]); #endif //implementation bool leap(short int year) { bool l=0; //year divisible by 4 but not by 100 or it is divisible by 400 then leap yea r if((year%4==0 && year%100!=0) year%400==0) { l=1; } return l; } bool checkvalid(short int day,short int mon,short int year) { bool valid=1; if(day<=0 year<0) { valid=0; } if(valid) switch(mon) { case 1: case 3: case 5: case 7:

case 8: case 10: case 12: if(day>31) { valid=0; } break; case 4: case 6: case 9: case 11: if(day>30) { valid=0; } break; case 2: if((leap(year)&&day>29) valid? mec { valid=0; } break; default: valid=0; } return valid; } short int get_month(char mm[4]) { short int mon; if(strcmp(mm,"Jan")==0) mon=1; else if(strcmp(mm,"Feb")==0) mon=2; else if(strcmp(mm,"Mar")==0) mon=3; else if(strcmp(mm,"Apr")==0) mon=4; else if(strcmp(mm,"May")==0) mon=5; else if(strcmp(mm,"Jun")==0) mon=6; else if(strcmp(mm,"Jul")==0) mon=7; else if(strcmp(mm,"Aug")==0) mon=8; else if(strcmp(mm,"Sep")==0) mon=9; else if(strcmp(mm,"Oct")==0) mon=10; else if(strcmp(mm,"Nov")==0) mon=11; else if(strcmp(mm,"Dec")==0) mon=12; else mon=0; return mon; }

(!leap(year)&&day>28))//leap? #days?

MyDate::MyDate(short int dd,short int mm,short int yyyy):day(dd),mon(mm),year(yy yy)

{ } MyDate::MyDate(const MyDate &md):day(md.day),mon(md.mon),year(md.year) { } MyDate MyDate::operator=(const MyDate &md) { day=md.day; mon=md.mon; year=md.year; return *this; } bool MyDate::operator==(const MyDate &md) { return day==md.day && mon==md.mon && year==md.year ; } bool MyDate::operator>(const MyDate &md) { return year>md.year (year==md.year && mon>md.mon) n==md.mon && day>md.day) ; } bool MyDate::operator<(const MyDate &md) { return year<md.year (year==md.year && mon<md.mon) n==md.mon && day<md.day) ; } bool MyDate::operator>=(const MyDate &md) { return this->operator==(md) this->operator>(md); } bool MyDate::operator<=(const MyDate &md) { return this->operator==(md) this->operator<(md); } MyDate MyDate::operator++() { ++day; switch(mon) { case 1: case 3: case 5: case 7: case 8: case 10:if(day>31) { day=1; ++mon; } break; case 12:if(day>31) { mon=1; day=1;

(year==md.year && mo

(year==md.year && mo

++year; } break; case case case case 4: 6: 9: 11: if(day>30) { day=1; ++mon; } break; case 2: if((leap(year) && day>29) { day=1; ++mon; } } return *this; } MyDate MyDate::operator--() { if(day>1) { --day; } else //day=1 { switch(mon) { case 1: day=31; mon=12; --year; break; case 3: --mon; if(leap(year)) { day=29; } else { day=28; } break; case 5: case 7: case 10: case 12: day=30; --mon; break; case 2: case 8: case 4: case 6: case 9: case 11:day=31; --mon; break; }

(!leap(year) && day>28))

} return *this; } MyDate MyDate::operator++(int n) { ++*this; return *this; } MyDate MyDate::operator--(int n) { --*this; return *this; } //-&+nos day-=no if<0 then --month , day=(largest day in that month + day) ifmo n<0 --year mon=12 day=(largest day in that month + day) void MyDate::dispdate() { cout<<day<<'/'<<mon<<'/'<<year<<endl; } MyDate::~MyDate() { } //client file int main() { short int day,year,month; bool valid; char mon[4]; cout << "Enter a date (enter month as int): \n"; do { cin>>day>>month>>year; }while(!checkvalid(day,month,year)); MyDate m1(day,month,year); cout<<"Date1 : "; m1.dispdate(); cout << "Enter another date (enter month as string): \n"; do { cin>>day>>mon>>year; month=get_month(mon); }while(!checkvalid(day,month,year)); MyDate m2(day,month,year); cout<<"Date2 : "; m2.dispdate(); if(m1>m2) { cout<<"date1 > date2\n"; } if(m1<m2) { cout<<"date1 < date2\n"; }

if(m1==m2) { cout<<"date1 = date2\n"; } if(m1>=m2) { cout<<"date1 >= date2\n"; } if(m1<=m2) { cout<<"date1 <= date2\n"; } cout<<"decrementing date1 and displaying: \n"; for(int i=0;i<31;++i) { --m1; m1.dispdate(); } cout<<"\nincrementing date2 and displaying : \n"; for(int i=0;i<31;++i) { m2++; m2.dispdate(); } return 0; }

You might also like