Maybe i'm not getting the concept of OOP, but I was assigned a project.
Create a class called date that contains three members: the month (int), the day of the month (int) and the year (int). Have the user enter two dates and store tehse dates in two sperate objects. Limit input from 1900 years or more. Find the difference between the two dates entered by the user in the number of months, day sand years. Store this information in a third Date object. Assume a month has 30days and a year has 365 days.
Write several functions as part of your Date class:
1) a default constructor and a 3 argument copy constructor.
2) a function to display each Date object.
3) a function to calculate the difference between two dates in months, days and years.
here is what I have;
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
typedef struct datedata
{
int day;
int month;
int year;
} DATEDATA;
class date
{
private:
DATEDATA theDate;
public:
void setData(void);
DATEDATA getData(void);
DATEDATA date::dateDiff(DATEDATA, DATEDATA);
};
DATEDATA date::getData( void )
{
return (theDate);
}
DATEDATA date::dateDiff(DATEDATA date1, DATEDATA date2)
{
DATEDATA tmpDate;
unsigned long days1;
unsigned long days2;
unsigned long diffDays;
days1 = (365 * date1.year) + (30 * date1.month) + (date1.day);
days2 = (365 * date2.year) + (30 * date2.month) + (date2.day);
diffDays = (days1 > days2) ? (days1 - days2) : (days2 - days1);
tmpDate.year = (diffDays / 365);
tmpDate.month = ((diffDays % 365) / 30);
tmpDate.day = ((diffDays % 365) % 30);
return tmpDate;
}
void date::setData( void )
{
int day;
int month;
int year;
bool fail = true;
while (fail)
{
fail = false;
cin.clear();
cout << endl << "Please enter a day:";
cin >> day;
cout << "Please enter a month:";
cin >> month;
cout << "Please enter a year:";
cin >> year;
if (day < 0 || day > 30)
{
cout << endl << "Day value out of range, acceptable is (1-30) days." << endl;
fail = true;
}
if (month < 0 || month > 12)
{
cout << endl << "Month value out of range, acceptable range is (1-12) months." << endl;
fail = true;
}
if (year < 1900)
{
cout << endl << "Year out of range, year must be greater than 1900" << endl;
fail = true;
}
}
theDate.day = day;
theDate.month = month;
theDate.year = year;
}
int main ( void )
{
date obj1,obj2,obj3;
DATEDATA theDiff;
cout << endl << "You are going to be prompted for two dates.";
cout << endl << "This program will calculate the difference between the two dates" << endl;
obj1.setData();
obj2.setData();
theDiff = obj3.dateDiff(obj1.getData() , obj2.getData());
cout << endl << "The difference was " << theDiff.year << " year(s), " << theDiff.month << " month(s)";
cout << ", " << theDiff.day << " day(s)" << endl;
system("PAUSE");
return 0;
}
How exactly am I supposed to link it all together...??
-Brian