Define a class called Date with month, day and year as its only integer data. Ma
ID: 3704152 • Letter: D
Question
Define a class called Date with month, day and year as its only integer data. Make sure the class can be derived from.
Include a constructor with default values for all 3 data members, such as 1, 1, 1900, a destructor and a set() member function that takes 3 parameters for month, day and year and uses them to set month, day and year to the values passed to it.
Include non-member friend functions for overloading << and >> for outputting from and inputting values into Date objects.
Also, overload >, < and == to compare two dates, returning true or false, depending on the result of the comparison.
Then, publicly derive a class called hDate from Date. hDate will have an additional data member called holiday of boolean type, in addition to inheriting Date's data.
Overload << and >> operators (remeber hDate has month, day, year and holiday members). When inputting data, the user must be asked if the entered data is a holiday or not, and when displaying, it must show if it's a holiday.
Include for hDate a default constructor which calls the default constructor of Date and sets the holiday to false, as well as a constructor that takes 4 parameters: month, day, year and holiday, passing the first parameters to Date's constructor. Also, include a destructor to delete any dynamically allocated objects.
Define a class called hDateArray with two private data members: an integer called size indicating the number of hDate objects in the array and an hDate pointer called ptr to point to the first element of the array.
Define a constructor for it that takes an integer for the array size as a parameter and dynamically allocates size hDates, assigning the returned pointer to ptr.
Also, include a destructor that will release the dynamically allocated array.
Also, include a function called input() for inputting size hDates into the array and a function called output() for displaying all the hDate objects within the array.
The main program is provided to give you an idea of what is expected:
int main()
{
int size; //size fo the array
cout << "Enter a size for the array of dates: ";
cin >> size;
//create an object of hDateArray called dates with size parameter passed to its //constructor
hDateArray dates(size);
//dates as an object of hDateArray is an array of size hDate objects each derived from //Date class
dates.input(); //enter values into the array
cout << " You entered: ";
dates.output(); //display the dates entered along with if any of them is a holiday
cout << " Press any key to continue.";
_getch();
return 0;
}
must be c++ code please and thank you
Explanation / Answer
Hi Dear,
Please find my implementation.
File: Date.cpp
#include <iostream>
#include <iomanip>
using namespace std;
class Date
{
protected:
int month;
int day;
int year;
public:
Date() : month(1), day(1), year(1900) {}
Date(int a_month, int a_day, int a_year) : month(a_month), day(a_day), year(a_year) {}
~Date() {}
void set(int a_month, int a_day, int a_year)
{
month = a_month;
day = a_day;
year = a_year;
}
friend ostream & operator << (ostream & os, const Date & date)
{
os << setw(2) << setfill('0') << date.month << "/" << date.day << "/" << date.year << endl;
return os;
}
friend istream & operator >> (istream & is, Date & date)
{
is >> date.month >> date.day >> date.year;
return is;
}
bool operator < (const Date & date)
{
if (year < date.year) return true;
if (month < date.month) return true;
if (day < date.day) return true;
return false;
}
bool operator > (const Date & date)
{
if (year > date.year) return true;
if (month > date.month) return true;
if (day > date.day) return true;
return false;
}
bool operator == (const Date & date)
{
if (year != date.year && month != date.month && day != date.day)
return true;
return false;
}
};
class hDate : public Date
{
private:
bool holiday;
public:
hDate() : Date(), holiday(false) {}
hDate(int a_month, int a_day, int a_year, bool a_holiday)
: Date(a_month, a_day, a_year), holiday(a_holiday) {}
~hDate() {}
friend ostream & operator << (ostream & os, const hDate & date)
{
os << setw(2) << setfill('0') << date.month << "/" << date.day << "/" << date.year;
if (date.holiday == true)
{
os << " (holiday: yes)" << endl;
}
else
{
os << " (holiday: no)" << endl;
}
return os;
}
friend istream & operator >> (istream & is, hDate & date)
{
is >> date.month >> date.day >> date.year >> boolalpha >> date.holiday;
return is;
}
};
class hDateArray
{
private:
int size;
hDate * ptr;
public:
hDateArray(int a_size) : size(a_size)
{
ptr = new hDate[size];
}
~hDateArray()
{
if (ptr != NULL) delete[] ptr;
}
void input()
{
cout << "Enter " << size << " dates in format: mm dd yyyy holiday" << endl;
cout << "(Note: For holiday enter true or false boolean value)" << endl;
for (int i = 0; i < size; i++)
{
cin >> ptr[i];
}
}
void output()
{
for (int i = 0; i < size; i++)
{
cout << ptr[i];
}
}
};
int main()
{
int size; //size fo the array
cout << "Enter a size for the array of dates: ";
cin >> size;
hDateArray dates(size);
dates.input();
cout << " You entered: ";
dates.output();
return 0;
}
Sample Execution Output:
Enter a size for the array of dates: 3
Enter 3 dates in format: mm dd yyyy holiday
(Note: For holiday enter true or false boolean value)
01 01 2017 true
02 02 2017 false
31 01 2017 false
You entered:
01/1/2017 (holiday: yes)
02/2/2017 (holiday: no)
31/1/2017 (holiday: no)
Please DONT forgot to rate my answer. We are working hard for you Guy!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.