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

C++ Please. I only need R9.3, I just thought posting R9.2 would help. My teacher

ID: 3901903 • Letter: C

Question

C++ Please. I only need R9.3, I just thought posting R9.2 would help. My teacher wants me to use the member functions she gave. Thank you.

Consider a Date class that stores a calendar date such as November 20, 2011. Consider two possible implementations of this class: one storing the day, month, and year, and another storing the number of days since January 1, 1900. Why might an implementor prefer the second version? How does the choice affect the user of the class? R9.2 Write a partial C++ class definition that contains the public interface of the Date class described in ExerciseR9.2. Supply member functions for setting the date to a particular year, month, and day, for advancing the date by a given number of days, and for finding the number of days betweein this date and another. Pay attention to const. R9.3 Implement the following member functions Date: Date (int year, int month, int day): void Date::advance_days (int days); int Date::difference_in_days(Date d) const; for the private data members, you may want to use the second method described in r9.2 Useful link: http://www.cplusplus.com/reference/ctime/tm/

Explanation / Answer

#include<iostream>
using namespace std;

// To store number of days in all months from January to Dec.
const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

// Class Date definition
class Date
{
// Data member to store day, month and year
int day, month, year;
public:
// Prototype of member functions
Date(int, int, int);
int countLeapYears(Date);
int getDifference(Date, Date);
void displayDate();
};// End of class

// Parameterized constructor to initialize data member to parameter value
Date::Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}// End of constructor

// Function to display date
void Date::displayDate()
{
// Checks the month number and display it in string form
switch(month)
{
case 1:
cout<<"January ";
break;
case 2:
cout<<"February ";
break;
case 3:
cout<<"March ";
break;
case 4:
cout<<"April ";
break;
case 5:
cout<<"May ";
break;
case 6:
cout<<"June ";
break;
case 7:
cout<<"July ";
break;
case 8:
cout<<"August ";
break;
case 9:
cout<<"September ";
break;
case 10:
cout<<"October ";
break;
case 11:
cout<<"November ";
break;
case 12:
cout<<"December ";
break;
}// End of switch case
// Displays day and year
cout<<day<<", "<<year<<endl;
}// End of function

// Function counts number of leap years before the given date
int Date::countLeapYears(Date date)
{
int y = date.year;

// Check if the current year needs to be considered for the count of leap years or not
if (date.month <= 2)
y--;

// An year is a leap year if it is a multiple of 4, multiple of 400 and not a multiple of 100.
return y / 4 - y / 100 + y / 400;
}// End of function

// Function returns number of days between two given dates
int Date::getDifference(Date date1, Date date2)
{
// Count total number of days before first date 'date1'

// Initialize count using years and day
long int numDay1 = date1.year * 365 + date1.day;

// Add days for months in given date
for (int c = 0; c < date1.month - 1; c++)
numDay1 += monthDays[c];

// Since every leap year is of 366 days, add a day for every leap year
numDay1 += countLeapYears(date1);

// Counts total number of days before date 'date2'

long int numDay2 = date2.year * 365 + date2.day;
for (int c = 0; c < date2.month - 1; c++)
numDay2 += monthDays[c];
numDay2 += countLeapYears(date2);

// return difference between two counts
return (numDay2 - numDay1);
}// End of function

// main function definition
int main()
{
// date1 object created using parameterized constructor
Date date1(15, 3, 2005);
// date2 object created using parameterized constructor
Date date2(10, 4, 2010);

// Call the function to display date1
date1.displayDate();
// Call the function to display date1
date2.displayDate();

// Call the function to display number of days between two given dates
cout << " Difference between two dates is = " << date1.getDifference(date1, date2);
return 0;
}// End of main function


Sample Ouput:

March 15, 2005
April 10, 2010

Difference between two dates is = 1852

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