C++ Date Class: Calculate Differences and Add Days
C++ Date Class with Operator Overloading
The following program defines a date class in C++ that calculates the difference between two dates, adds a number of days to a date, and handles leap years correctly.
Source Code
#include<iostream.h>
#include<conio.h>
#include<process.h>
class date
{
private:
int flag, day, month, year;
public:
date(int d, int m, int y)
{
day = d, month = m, year = y;
if (year % 4 == 0)
flag = 1;
else
flag = 0;
}
int operator-(date d2);
date operator+(int n);
int operator>(date d2);
friend ostream& operator<<(ostream& out, date d1);
int return_integer_date(date d1);
};
int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int b[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int date::return_integer_date(date d1)
{
int int_value = d1.day;
if ((d1.flag == 1) && (d1.month > 2))
{
for (int i = 0; i < d1.month; i++)
int_value = int_value + a[i];
int_value++;
}
else
for (int i = 0; i < d1.month; i++)
int_value = int_value + a[i];
return int_value;
}
int date::operator>(date d2)
{
int x = 1;
date d1(day, month, year);
if (d1.day == d2.day && d1.month == d2.month && d1.year == d2.year)
return x;
else if (d1.year > d2.year)
return x;
else if (d1.month > d2.month)
return x;
else if (d1.day > d2.day)
return x;
else
return 0;
}
int date::operator-(date d2)
{
int a1, a2, x = 0;
date d1(day, month, year);
if (d1.day == d2.day && d1.month == d2.month && d1.year == d2.year)
return x;
a1 = return_integer_date(d1);
a2 = return_integer_date(d2);
for (int i = d1.year - 1; i > d2.year; i--)
{
if (i % 4 == 0)
x = x + 366;
else
x = x + 365;
}
if (d1.year == d2.year)
x = x + (a1 - a2);
else
{
x = x + a1;
if (d2.year % 4 == 0)
x = x + (366 - a2);
else
x = x + (365 - a2);
}
return x;
}
date date::operator+(int n)
{
date d(day, month, year);
for (int i = 0; i < n; i++)
{
d.day++;
if (d.year % 4 == 0)
{
d.flag = 1;
if (d.day > b[d.month])
{
d.day = 1;
d.month++;
}
}
else
{
d.flag = 0;
if (d.day > a[d.month])
{
d.day = 1;
d.month++;
}
}
if (d.month > 12)
{
d.month = 1;
d.year++;
}
}
return d;
}
ostream& operator<<(ostream& out, date d1)
{
out << d1.day << "/" << d1.month << "/" << d1.year;
return out;
}
void main()
{
int d, m, y, no_of_days;
clrscr();
cout << "Enter a valid date\n";
cout << "Enter day (dd): ";
cin >> d;
cout << "Enter month (mm): ";
cin >> m;
cout << "Enter year (yyyy): ";
cin >> y;
cout << endl;
date d1(d, m, y);
cout << "Enter another date\n";
cout << "Enter day (dd): ";
cin >> d;
cout << "Enter month (mm): ";
cin >> m;
cout << "Enter year (yyyy): ";
cin >> y;
cout << endl;
date d2(d, m, y);
cout << "First date entered: " << d1 << endl << endl;
cout << "Second date entered: " << d2 << endl << endl;
if (d1 > d2)
{
no_of_days = d1 - d2;
}
else
no_of_days = d2 - d1;
cout << "The difference between the two dates: ";
cout << no_of_days << " DAYS" << endl << endl;
getche();
cout << "Enter a number: ";
cin >> no_of_days;
d1 = d2 + no_of_days;
cout << "\nThe resulting date from the date " << d2 << " is " << d1 << endl;
getch();
}Key Features
- Date difference: Computes the number of days between two dates using the
-operator. - Date comparison: Determines which date is later using the
>operator. - Date addition: Adds a given number of days to a date using the
+operator. - Leap year handling: Accounts for February 29 in leap years.
Notes
This code uses iostream.h, conio.h, and void main(), which are non-standard and deprecated. For modern compilers, use <iostream>, remove conio.h, and use int main().