Hi,
I am to write a C++ date class.
I have set up the class. I am able to add and substract to the number of years.
The requirements are that I have to do the same for the number of days and the number of months as well. I am not really sure how to go about doing that part.
The default date is 1/1/2000, so if I add 31 days, the new date should be 2/1/2000. Similary if the date is 1/1/2000 and I add 24 months the new date should be 1/1/2002 (actually 1/2/2002 since 2000 is a leap year).
The starting date is always 1/1/2000 as it is the default.
I have attached my code below. This is what I have so far for the class.
class date{
private:
int curr_day; // this block for default
int curr_month;
int curr_year;
int new_day; //this block for changes
int new_month;
int new_year;
int month; //this block for comparisons
int day;
int year;
string date1;
public:
date(){
//sets the default date to 1/1/2000
curr_day = 1;
curr_month = 1;
curr_year = 2000;
new_day = curr_day;
new_month = curr_month;
new_year = curr_year;
//print_Date();
}
//MODIFIER Functions
//add days to default date
void add_days(int num_days){
}//end
//substract days from default date
void substr_date(int num_days){
}//end
//add months t default date
void add_months(int num_months){
int temp = curr_month + num_months;
if (temp <= 12)
new_month = temp;
else
{
}
}//end
//substract months from defualt date
void substr_months (int num_months){
int temp = curr_month - num_months;
if ((temp <= 12) && (temp >= 1))
new_month = temp;
}//end
//add years to default date
void add_years (int num_years) {
new_year = curr_year + num_years;
//print
}//end
//substract years from default date
void subtr_years (int num_years) {
new_year = curr_year - num_years;
}//end
//get size of month
int check_month(int ind_month){
switch (ind_month){
case 1 : return 31;
break;
case 2 : { if (isLeapYear())
return 29;
else
return 28;
break;
}
case 3 : return 31;
break;
case 4 : return 30;
break;
case 5 : return 31;
break;
case 6 : return 30;
break;
case 7 : return 31;
break;
case 8 : return 31;
break;
case 9 : return 30;
break;
case 10: return 30;
case 11: return 31;
break;
case 12: return 31;
break;
}//end switch
}//end
bool isLeapYear(){
if (isLeapYear(curr_year))
return true;
else
return false;
}//end
bool isLeapYear(int year){
if ( (year%4 == 0) && (year%100 == 0) && (year%400 == 0))
return true;
else
return false;
}//end
void print_new_Date(){
cout << setw(20) << "\t" << new_month <<"/" <<new_day << "/" << new_year;
if (isLeapYear(new_year))
cout << " Leap Year Too!!" << endl <<endl;
}//end
void print_Date(){
if (isLeapYear(curr_year))
cout << setw(27) << " Leap Year!!" << endl;
cout << setw(10) << "\t" << curr_month <<"/" << curr_day << "/" << curr_year;
}