Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose is to practice writing operator overloading and friend functions. Some parts are filled in others are not I need help with what to

The purpose is to practice writing operator overloading and friend functions.

Some parts are filled in others are not I need help with what to fill in and how.

-- Postfix decrement operator that decrements the objects day member

<< couts stream insertion operator that causes the date to be displayed in the form March 15, 2017

>> cins stream extraction operator that prompts the user for a date to be stored in a Date object

> Greater than relational operator that returns true if a Date object d1 is greater than another object d2 and false otherwise

- Subtraction operator. If one Date object is subtracted from another Date object, this operator should return the number of days between the two classes. For example, if April 10, 2014 is subtracted from April 18, 2014, the result will be 8

Note: the class should detect the following conditions and handle them accordingly:

When a date is set to the last day of the month and incremented, it should become the first day of the following month

When a date is set to December 31 and incremented, it should become January 1 of the following year

When a day is set to the first day of the month and decremented, it should become the last day of the previous month

When a date is set to January 1 and decremented, it should become December 31 of the previous year.

Complete the following program based on the given comments (SHOWN IN RED). Some of the functions are already implemented.

#include

#include

using namespace std;

class Date; // Forward declaration

// Function prototypes

Date operator ++(Date &); // Prefix ++

Date operator --(Date &, int); // Postfix --

ostream &operator <<(ostream &, const Date &); // << operator

istream &operator >>(istream &, Date &); // >> operator

bool operator >(const Date &, const Date &);// greater than operator

int operator -(const Date &, const Date &);// Subtraction operator

// Constants

const int NUM_MONTHS = 12;

// Declaration of the Date class

class Date

{

// Make all the functions whose prototypes are defined

// above friends of this class

private:

int month; // To hold the month

int day; // To hold the day

int year; // To hold the year

// An array of strings to hold the names of the months

string names[NUM_MONTHS];

// An array to hold the number of days that each month has.

int numDays[NUM_MONTHS];

// Private member function to assign the month names

// to the names array

void setNames();

// Private member function to assign the numbers of

// days to the numDays array

void setDays();

public:

// Constructors

Date();

Date(int, int, int);

// Mutators

void setMonth(int m);

void setDay(int d);

void setYear(int y);

// Other member functions

void showDate1();

void showDate2();

bool isLeapYear(int); // OPTIONAL

};

//**********************************

// Function Definitions *

//**********************************

// default constructor

Date::Date()

{

setNames();

setDays();

}

// Overloaded constructor

Date::Date(int m, int d, int y)

{

setMonth(m);

setDay(d);

setYear(y);

setNames();

setDays();

}

// Member function setNames

void Date::setNames()

{

names[0] = "January";

names[1] = "Febraury";

names[2] = "March";

names[3] = "April";

names[4] = "May";

names[5] = "June";

names[6] = "July";

names[7] = "August";

names[8] = "September";

names[9] = "October";

names[10] = "November";

names[11] = "December";

}

// Member function setDays

void Date::setDays()

{

numDays[0] = 31;

numDays[1] = 28;

numDays[2] = 31;

numDays[3] = 30;

numDays[4] = 31;

numDays[5] = 30;

numDays[6] = 31;

numDays[7] = 31;

numDays[8] = 30;

numDays[9] = 31;

numDays[10] = 30;

numDays[11] = 31;

}

// Member function setMonth

void Date::setMonth(int m)

{

if (m >= 1 && m <= 12)

month = m;

else

{

cout << m << " is not a valid value for the month. ";

exit(EXIT_FAILURE);

}

}

// Member function setDay

void Date::setDay(int d)

{

if (d >= 1 && d <= 31)

day = d;

else

{

cout << d << " is not a valid value for the day. ";

exit(EXIT_FAILURE);

}

}

// Member function setYear

void Date::setYear(int y)

{

if (y < 1900)

{

cout << "Enter a 4 digit year of 1900 or greater. ";

exit(EXIT_FAILURE);

}

else

year = y;

}

// Member function showDate1: Displays the date

// in the form MM/DD/YY Example: 12/25/2014

void Date::showDate1()

{

cout << month << "/" << day << "/" << year;

}

// Member function showDate2

// Displays the date in the following form: December 25, 2014

void Date::showDate2()

{

cout << names[month+1] << " " << day << ", ";

cout << year << endl;

}

// Member function isLeapYear is OPTIONAL

bool Date::isLeapYear(int year)

{

if (year % 4 == 0)

return true;

else return false;

}

// Implement the Prefix ++ operator

Date operator ++( Date & d)

{

}

// Implement the Prostfix -- operator

Date operator --(Date & d, int)

{

}

// Implement the Overloaded << operator

ostream &operator <<(ostream &strm, const Date &obj)

{

}

// Implement the Overloaded >> operator

istream &operator >>(istream &strm, Date &obj)

{

}

// Implement the Overloaded > operator

bool operator > (const Date & d1, Date & d2)

{

}

// Implement the Overloaded - operator

int operator -(const Date & d1, const Date & d2)

{

/* Note: you need to compare d1 and d2 to determine which on is greater. You may perform (greater - lesser) and negate the difference if d2 is the greater at the end.

If the lesser date is from an earlier year than the greater date:

add up the days from the lesser date to the end of its year

add up the days in the years between the lesser to the greater

add up the days from the beginning of the greater year until the start of the month held by the greater year

add up the days from the start of the greater date's month through that date's current day.

*/

}

//*************************************

// Function main *

//*************************************

int main()

{

Date d1(2, 2, 2006);

Date d2(11, 10, 2003);

// Demonstrate the overloaded - and << operators by subtracting d2 from d1 and printing d1, d2, and the subtraction result.

// Demonstrate the overloaded ++ and << operators by incrementing and printing d1.

// set d1s day to 31 and month to 12. Increment then print.

// Demonstrate the overloaded operator:

// set d1s day to 10 and month to 7 and year to 2003.

// Decrement then print.

// set d1s day to 1 and month to 1 and year to 2004

// Decrement then print.

// Demonstrate the overloaded >> operator by entering a new date

// and printing it.

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions