i am juss kinda stuck in doing my assignment and i am almost through doin it except the code responsible for calculating the difference between two date objects
could anyone help me out here that wuill be really appreciate
here my code
#ifndef DATE_H
#define DATE_H
class Date {
private:
int day;
int month;
int year;
public:
Date();
Date(int a,int b, int c);
void Display();
void operator +=(int a); //Add number of days to date object
Date operator-(const Date &x,const Date &y); //****
//**** could i use anything like this for calculating the differences between two date ?? objects
bool valid(const int year,const int month, const int day);
~Date();
};
#endif
#include<iostream>
using namespace std;
#include"Date.h"
Date::Date()
{ month=1;
day=1;
year=1;
}
Date::Date(int a,int b, int c)
{
if (!valid(a,b,c))
cout << " Date is invalid " <<endl;
else
year=a;
month=b;
day=c;
}
bool Date::valid(const int year,const int month, const int day)
{
if (year<0)
return false;
if (month >12 || month <1)
return false;
if (day >31 || day <1)
return false;
if ((day ==31 &&( month ==2 || month ==4 || month ==6 || month ==9 || month ==11) ) )
return false;
if (day ==30 && month ==2)
return false;
if (year <2000){
if ((day ==29 && month ==2) && !(((year-1900)%4==0)) )
return false;
}
if (year >2000){
if ((day ==29 && month ==2) && !(((year-2000)%4==0)))
return false;
}
return true;
}
void Date::Display()
{
static char *name[] = {"null", "January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December" };
cout <<'\n' << name[month] << ' ' << day << "," << year << '\n';
}
Date::~Date()
{
cout<< "Static date objects are deleted "<<endl;
}
void Date::operator+=(int a)
{
day+=a;
const int month_days[ ] = {0, 31, 28, 31, 30, 31, 30 , 31, 31 , 30, 31, 30, 31};
if (day == month_days[month]) // is it last day of the month?
{ day = 1;
month = month % 12 + 1;
}
else day++;
}

New Topic/Question
Reply




MultiQuote





|