Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program C++: operator overloading and friend functions Operator functions fo

ID: 3591755 • Letter: C

Question

C++ Program

C++: operator overloading and friend functions Operator functions for the Data class provided +Prefix increment operator that increments the object's day member -- Postfix decrement operator that decrements the object's day member >cin's 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 fronm April 18, 2014, the result will be 8 Functions 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

Explanation / Answer

Date.hpp

#include <iostream>

#include <string>

class Date;

const int NUM_MONTHS = 12;

class Date

{

private :

int month;

int day;

int year;

string names[NUM_MONTHS];

int numDays[NUM_MONTHS];

void setNames();

void setDays();

public:

Date();

Date(int,int,int);

void setMonth(int m);

void setDay(int d);

void setYear(int y);

void showDate1();

void showDate2();

void operator++();

void operator--();

void operator++();

void operator--();

bool operator>(Date &d);

int operator-(Date &d);

friend ostream &operator<<( ostream &output, Date &d);

friend istream &operator>>( istream &input, Date &d);

};

Date.cpp

#inlcude<iostream>

#include<string>

#include "Date.hpp"

Date :: Date()

{

setNames();

setDays();

}

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

{

setMonth(m);

setDay(d);

setYear(y);

setNames();

setDays();

}

void Date :: setMonth (int m)

{

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

month = m;

else

{

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

}

}

void Date :: setDay(int d)

{

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

day = d;

else

{

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

}

}

void Date :: setYear( int y)

{

if(y<1900)

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

else

year = y;

}

void Date :: showDate1()

{

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

}

void Date :: showDate2()

{

std :: cout << names[month-1] <<" "<<day<<",";

std :: cout << year <<std :: endl;

}

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";

}

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;

}

void Date :: operator++()

{

if( (day == 31) && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)

{

if(month + 1 > 12 )

{

month = 1;

year = year + 1;

day = 1;

}

}

else if ( (day == 30) && ( month == 4 || month == 6 || month == 9 || month == 11 ))

{

day = 1;

month = month + 1;

}

else if(day == 28 && month == 2)

{

day = 1;

month = month + 1;

}

}

void Date :: operator--()

{

if((day == 1) && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))

{

if(month == 1)

{

day = 31;

month = 12;

year = year + 1;

}

esle if(month == 8)

{

day = 31;

month = 7;

}

esle if(month == 3)

{

day = 28;

month = 2;

}

else

{

day = 30;

month = month -1;

}

}

}

ostream Date :: &operator<<(ostream &output, Date &d)

{

output << names[month-1] << " " << day << " "<<year;

return output;

}

istream Date :: &operator>>(istream &input, Date &d)

{

int da; int m; int y;

std :: cout << "enter date";

std :: cin >> da;

std :: cout << "enter month";

std :: cin >> m;

std :: cout << "enter year";

std :: cin >> y;

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

day = da;

else

{

std :: cout << da << "is not a valid value for the day. ";

}

if(y<1900)

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

else

year = y;

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

month = m;

else

{

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

}

input >>da>>m>>y;

return input;

}

bool Date :: operator>(Date &d)

{

if(year >= d.year)

{

if(year == d.year)

{

if(month >= d.month)

{

if(month == d.month)

{

if(day > d.day)

{

return true;

}

else

return false;

}

else

{

return false;

}

}

else

{

return false;

}

}

else

{

return true;

}

}

else

{

return false;

}

}

int Date :: operator-(Date &d)

{

int d1;

d1 = day - d.day;

if(d1>=0)

return d1;

else

return -d1;

}

Main.cpp

#include<iostream>

#include<string>

#inlcude "Date.hpp"

int main()

{

Date d1(2,2,2006);

Date d2(11,10,2003);

std :: cout << d1 << d2 << d1-d2;

++d1;

std :: cout << d1;

d1.setDay(31);

d1.setMonth(12);

++d1;

std :: cout << d1;

d1.setDay(1);

d1.setMonth(1);

d1.setYear(2004);

d1--;

Date d3;

std :: cin >> d3;

d3.showDate1();

return 0;

}

/* I didn't mention any comments because it is already in the problem*/

/* if any doubts please comment*/

/* thank you*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote