CS1337 Homework #4-3 Assigned Mar 30, due April 6 at 11:59 PM This homework assi
ID: 3812171 • Letter: C
Question
CS1337 Homework #4-3 Assigned Mar 30, due April 6 at 11:59 PM This homework assignment gives you the opportunity to practice structures, classes, member variables, member functions, static member variables and static member functions and object composition. It builds upon HW4-2. There are two parts in this assignment: part A and part B. If you implement partAonly, you can earn up to 100 points out of 100. If you implement part A and part B, you can earn up to 120 points out of 100. Make sure you have part A working before you attempt part B. You need partAfor part B to work. Hw4-3-Part A Your program should implement the following: 1. Date class Implement a Date class that has the following private member variables: month of type int day of type int year of type int and the following public member functions set month day year mutator that sets the variables month day and year to the parameter values overloaded relational operator. (dl d2) is true if and only if date d2 comes after date dl, chronologically. For example, (3/8/2017) (3/9/2017. overloaded relational operator (d1 d2) is true if and only if date d2 is equal to di overloaded K- relational operator. (dl C- d2) is true if and only if (dl d2) is true, or (d1 d2) is true print Prints the date in the format month/day/year. For example, 3/9/2017. Do not write a 2. Driver for the Date class (in main function) For consistency in the submissions to make the grading task more efficient, copy and paste this code into your program const int NUM DATES string datearrayINUM DAIES) 02/12/2017 02/12/2018 02/13/2017 "03/12/2017" Driver for the Date clasaExplanation / Answer
Solution for PART A:
Please post PART B separately:
C++ Code:
#include <iostream>
#include <string>
using namespace std;
class Date
{
private:
int month;
int day;
int year;
public:
void set(int month_, int day_,int year_)
{
month=month_;
day=day_;
year=year_;
}
void print()
{
//print the date
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
bool operator<(const Date& b)
{
if(this->year<b.year)
return true;
else if (this->year>b.year)
return false;
else
{
if(this->month<b.month)
return true;
else if (this->month>b.month)
return false;
else
{
if(this->day<b.day)
return true;
else
return false;
}
}
}
bool operator==(const Date& b)
{
if(this->year == b.year && this->month == b.month && this->day == b.day)
return true;
else
return false;
}
bool operator<=(const Date& b)
{
if(this->year<b.year)
return true;
else if (this->year>b.year)
return false;
else
{
if(this->month<b.month)
return true;
else if (this->month>b.month)
return false;
else
{
if(this->day<b.day)
return true;
else if (this->day==b.day)
return true;
else
return false;
}
}
}
};
int getMonth(string d)
{
string da = d.substr (3,2);
int x=stoi(da);
return x;
}
int getDay(string d)
{
string da = d.substr (0,2);
int x=stoi(da);
return x;
}
int getYear(string d)
{
string da = d.substr (6,4);
int x=stoi(da);
return x;
}
const int NUM_DATES=4;
string dateArray[NUM_DATES]={"02/12/2017", "02/12/2018", "02/12/2013", "03/12/2017"};
int main()
{
cout << "**********************************" << endl;
cout << "PART A- Driver for the Date Class:" << endl;
cout << "**********************************" << endl;
Date d1, d2;
for(int i=0; i<NUM_DATES; i++)
for(int j=0; j<NUM_DATES; j++)
{
int month=getMonth(dateArray[i]);
int day=getDay(dateArray[i]);
int year=getYear(dateArray[i]);
d1.set(month, day, year);
cout<<" ------------------ ";
cout<<"d1: ";
d1.print();
month=getMonth(dateArray[j]);
day=getDay(dateArray[j]);
year=getYear(dateArray[j]);
d2.set(month, day, year);
cout<<" ------------------ ";
cout<<"d2: ";
d2.print();
cout<<endl;
if(d1<d2) cout<<"d1 < d2 ";
if(d2<d1) cout<<"d2 < d1 ";
if(d1==d2) cout<<"d1 == d2 ";
if(d1<=d2) cout<<"d1 <= d1 ";
if(d2<=d1) cout<<"d2 <= d1 ";
}
return 0;
}
Sample Output:
**********************************
PART A- Driver for the Date Class:
**********************************
------------------
d1: 2/12/2017
------------------
d2: 2/12/2017
d1 == d2
d1 <= d1
d2 <= d1
------------------
d1: 2/12/2017
------------------
d2: 2/12/2018
d1 < d2
d1 <= d1
------------------
d1: 2/12/2017
------------------
d2: 2/12/2013
d2 < d1
d2 <= d1
------------------
d1: 2/12/2017
------------------
d2: 3/12/2017
d1 < d2
d1 <= d1
------------------
d1: 2/12/2018
------------------
d2: 2/12/2017
d2 < d1
d2 <= d1
------------------
d1: 2/12/2018
------------------
d2: 2/12/2018
d1 == d2
d1 <= d1
d2 <= d1
------------------
d1: 2/12/2018
------------------
d2: 2/12/2013
d2 < d1
d2 <= d1
------------------
d1: 2/12/2018
------------------
d2: 3/12/2017
d2 < d1
d2 <= d1
.....