/* 5. Write a C++ program to create a class called as DATE. Accept two valid
dates in the form of dd/mm/yyyy. Implement the following by overloading the operators - and +. Display the results by overloading the operator << after
every operation.
(i). no_of_day = d1 - d2, where d1 and d2 are DATE objects; d1>=d2; and
no_of_days is an integer
(ii) d1 = d1 + no_of_days, where d1 is a DATE object and no_of_days is an integer */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class DATE
{
int days,month,year;
int a[13];
public:
DATE();
void readdate();
DATE operator +(int days);
int operator -(DATE d);
friend ostream& operator <<(ostream& print,DATE d);
};
DATE::DATE()
{
a[1]=31,a[2]=28,a[3]=31,a[4]=30,a[5]=31,a[6]=30,a[7]=31,a[8]=31,a[9]=30,a[10]=31,a[11]=30,a[12]=31;
}
void DATE::readdate()
{
cin>>days>>month>>year;
}
DATE DATE::operator +(int n)
{
for (int i=1; i<=n; i++)
{
days++;
if (days > a[month]) month++,days=1;
if (month > 12) year++,month=1;
}
return (*this);
}
int DATE::operator -(DATE d2)
{
int count=0;
if (year < d2.year)
{
cout<<"\nError : First date is less than second date.";
exit(0);
}
else if ((year == d2.year) && ((month < d2.month) || (days < d2.days)))
{
cout<<"\nError : First date is less than second date.";
exit(0);
}
if ((month == d2.month) && (year==d2.year))
count = days - d2.days;
else if (year == d2.year)
{
while (month != d2.month)
{
count++;
d2.days++;
if (d2.days > a[d2.month])
{
d2.month++;
d2.days=1;
}
}
count = count + days - 1;
}
else
{
while(1)
{
if ((year == d2.year) && (month == d2.month)) break;
count++;
d2.days++;
if (d2.days > a[d2.month])
{
d2.month++;
d2.days=1;
}
if (d2.month > 12)
{
d2.month=1;
d2.year++;
}
}
count=count+days-1;
}
return count;
}
ostream& operator <<(ostream& print,DATE d)
{
print<<d.days<<"/"<<d.month<<"/"<<d.year;
return print;
}
void main()
{
DATE d1,d2;
clrscr();
cout<<"\nEnter the first DATE in the form [DD MM YY]:";/*DATE 1 should be less than DATE 2*/
d1.readdate();
cout<<"\nEnter the second DATE in the form [DD MM YY]:";
d2.readdate();
int no_days=d1-d2;
cout<<"\nNo of days is:"<<no_days;
cout<<"\nEnter the no of days to be added to the first DATE :";
cin>>no_days;
d1=d1+no_days;
cout<<"\nAfter addindg days DATE becomes:";
cout<<d1;
getch();
}