I have to write two classes, date and event. The Date class will use integers to
ID: 3622371 • Letter: I
Question
I have to write two classes, date and event. The Date class will use integers to store the day, month, and year. For example, it might be used like this to keep track of the date for Thanksgiving in 2010, and New Years Eve 2010.Date *thanksgiving2010 = new Date(25, 11, 2010);
Date *newYearsEve2010 = new Date(31, 12, 2010);
The Event class uses date objects to represent the start and end of a multi-day event. Here is a line that constructs an event to represent the holiday season in 2010.
Event *holidays2010(thanksgiving2010, newYearsEve2010, “holiday season”);
Here are the classes public member functions, constructors, and class functions:
Date Class
Date(int day, int month, int year);
int getDay();
void setDay(int value);
int getMonth();
void setMonth(int value);
int getYear();
void setYear(int value);
static bool isLeapYear(int year);
static int daysInMonth(int month, int year);
Event Class
Event(Date *start, Date end, const char *title);
Date *getStartDate();
void setStartDate(Date *start);
Date *getEndDate();
void setEndDate(Date *start);
int eventDurationInDays();
the Date class has two static member functions. For the isLeapYear you should see if the year is evenly divisible by 4, but not by 1000. For the daysInMonth function, it was suggested that I give my Date class a static pointer to an array of 12 integers. Each element in the array should represent the days in the corresponding month. As well I am trying to experiment with making the Event class a friend of the Date class.
I have to submit five files: Date.h, Date.cpp, Event.h, Event.cpp, and testProgram.cpp. Someone helped me with the date classes so now I really just need the event classes. I really need help writing this so If any of this does not make sense please message me!! Thank you so much! Any help is greatly appreciated!
Explanation / Answer
//Date.h
//date class
//date.h
class Date
{
private:
int day, month, year;
public:
Date();
Date(int d, int m, int y);
int getDay() const;
void setDay(int value);
int getMonth() const;
void setMonth(int value);
int getYear() const;
void setYear(int value);
void showDate() const ;
void setDate(Date aDate);
int caln() const;
static bool isLeapYear(int year);
static int daysInMonth(int month, int year);
};
//Date.cpp
#include "Date.h"
#include <iostream>
Date::Date():day(0),month(0),year(0)
{
}
Date::Date(int d, int m, int y)
{
day=d;
month=m;
year=y;
};
void Date::setDate(Date aDate)
{
day=aDate.getDay();
month=aDate.getMonth();
year=aDate.getYear();
}
int Date::getDay() const
{
return day;
}
int Date::getMonth() const
{
return month;
}
int Date::getYear() const
{
return year;
}
void Date::setDay(int value)
{
day= value;
}
void Date::setMonth(int value)
{
month = value;
}
void Date::setYear(int value)
{
year = value;
}
void Date::showDate() const
{
std::cout<<month<<"/"<<day<<"/"<<year<<std::endl;
}
int Date::caln() const
{
return 1461*(month<=2 ? year-1 : year)/4
+ 153*(month<=2 ? month+13 : month+1)/5
+ day;
}
static bool isLeapYear(int year)
{
if((year%4==0 && year%100!=0) || (year%400==0)) return true;
return false;
}
static int daysInMonth(int month, int year)
{
const int daysPerMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if(month!=2) return daysPerMonth[month-1];
else
if(isLeapYear(year)) return 29;
else return 28;
}
//Event.h
#include "Date.h"
class Event
{
private:
Date _start, _end;
char _title[255];
public:
Event();
Event(Date *start, Date *end, const char *title);
Date *getStartDate() ;
void setStartDate(Date *start);
Date *getEndDate() ;
void setEndDate(Date *end);
char *getTitle() ;
void setTitle(char *title);
int eventDurationInDays();
void showEvent() const;
};
//Event.cpp
#include "Event.h"
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
Event::Event()
{
_start=Date(0,0,0);
_end=Date(0,0,0);
_title[0]='';
}
Event::Event(Date *start, Date *end, const char *title)
{
_start.setDate(*start);
_end.setDate(*end);
strcpy(_title,title);
}
Date *Event::getStartDate()
{
return &_start;
}
void Event::setStartDate(Date *start)
{
_start=*start;
}
Date *Event::getEndDate()
{
return &_end;
}
void Event::setEndDate(Date *end)
{
_end=*end;
}
char *Event::getTitle()
{
return _title;
}
void Event::setTitle(char *title)
{
strcpy(_title,title);
}
int Event::eventDurationInDays()
{
return _end.caln()-_start.caln();
}
void Event::showEvent() const
{
cout<<"Event "<<_title<<endl;
cout<<"start day: ";_start.showDate();
cout<<"end day: ";_end.showDate();
cout<<"Duration: "<<_end.caln()-_start.caln()<<" day(s)"<<endl;
}
//test Program
#include <iostream>
//#include "Date.h"
#include <cstdlib>
#include "Event.h"
using namespace std;
int main()
{
Date *thanksgiving2010 = new Date(25, 11, 2010);
Date *newYearsEve2010 = new Date(31, 12, 2010);
Date today(12,12,2010);
Event *holidays2010=new Event(thanksgiving2010, newYearsEve2010, "holiday season");
thanksgiving2010->showDate();
holidays2010->showEvent();
system("pause");
return 0;
}
This is full source code http://www.mediafire.com/?15naisof89m81cs
(I'm using the MS Visual C++ compiler )