The Questionis below a) Create an operator function for the Date class in Class
ID: 3688375 • Letter: T
Question
The Questionis below
a) Create an operator function for the Date class in Class 11.1 that compares two Date objects and returns a Boolean value of true if the first date is larger than the second; otherwise, it should return a false value. The "larger than" algorithm for this comparison operator function is the following: Accept one date as the current object being evaluated and a second date as an argument Determine the later date with the following procedure: Convert each date to a long integer value in the form yyyymmdd, using the algorithm in Exercise 2 Compare the corresponding integers for each date if the first date's long integer value is larger than the second dates's long integer vlaue. return true Else return false
below is my code i have so far is below the only thing i can't figure out is what to do in the main and what to to about comparing the dates to see which is larger please help as soon as possible because it is due today for homewor before midnight please
#include <iostream> //std io stream library, keyboard and terminal window
#include <fstream> //file stream library, i/o of files
#include <cstdlib> //needed for exit function
#include <string> //needed for strings
#include <iomanip> //needed for formatting
#include <vector> //needed for vectors
#include <algorithm> //needed for stl
#include <strstream>
#include <string>
using namespace std;
//declaration section
class Date
{
private:
int month;
int day;
int year;
public:
Date(int mm = 1, int dd = 1, int yyyy = 2014) //inline constructor
{month = mm; day = dd; year = yyyy;}
void setDate(int mm, int dd, int yy) //inline mutator
{month = mm; day = dd; year = yy;}
void showDate(); //accessor
bool isLeapYear();
bool operator==(const Date&);
bool checkDate();
long int convert();
};
//implementation section
void Date::showDate()
{
cout << "The Date is " << setfill('0') << setw(2) << month << ' / ' << setw(2) << day << ' / ' << setw(2) << year % 100;
cout << endl;
}
bool Date::isLeapYear()
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return true;
}
else
{
return false;
}
}
bool Date::operator==(const Date& date2)
{
if (day == date2.day && month == date2.month && year == date2.year)
{
return true;
}
else
{
return false;
}
}
long int Date::convert()
{
long int yyyymmdd = year * 10000 + month * 100 + day;
return yyyymmdd;
}
bool Date::checkDate()
{
long int yyyymmdd1 = isLeapYear();
long int yyyymmdd2 = isLeapYear();
if (yyyymmdd1 > yyyymmdd2)
{
return true;
}
else
{
return false;
}
}
int main()
{
Date a(2, 2, 2015);
Date b(2, 2, 2016);
cout << "The date is: " << a.showDate << endl;
b.isLeapYear();
cout << "The leap year date is: " << b.showDate << endl;
system("pause");
return 0;
}
Explanation / Answer
Sample code logic-
Long int date1 = date1_day + date1_month*100 + date1_year*10000;
Long int date2 = date2_day + date2_month*100 + date2_year*10000;
if(date1 > date2)
{
printf("date1 is greater than date2 ");
return true;
}
else
{
printf("date2 is greater than date1 ");
return false;
}
note- by using the above code logic in the given program to compare two dates in long int, the given question can be answered.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.