hi everybody, i would like to know how to computes and returns d2 – d1
d2 is passed to the function by reference and d1 is the implied calling object; date d1 is earlier than date d2
the function returns the difference between two date objects in terms of number of days, a positive integer
#include <iostream>
using namespace std;
class date {
public:
date();
date(int, int, int);
void showDate();
date addDays(int);
int diff(date &d2);
private:
int year;
int month;
int day;
};
date::date()
{}
date::date(int mm, int dd, int yy)
{
year = yy;
month = mm;
day = dd;
}
void date::showDate()
{
cout << month << '/' << day << '/' << year % 100;
}
date date::addDays(int n)
{
date hold = *this; // hold must initialized to the date of the calling object
hold.day = day + n; // which is pointed to by the "this" pointer
if (hold.day > 30) {
hold.day -= 30;
hold.month++;
}
if (hold.month > 12) {
hold.month -= 12;
hold.year++;
}
return hold;
}
int diff(date &d2) // this is the function that i need help with
{
}
int main()
{
date dObj(3, 10, 2010),d2,newDay;
dObj.showDate();
cout << endl;
newDay = dObj.addDays(30);
newDay.showDate();
newDay = dObj.diff(4, 10, 2010);
newDay.showDate();
cout << endl;
return 0;
}