C Help Objective To practice working with classes and class
C++ Help
Objective
To practice working with classes and class methods.
Tasks
Implement each of the class methods in the lab file:
Each of the setter functions should be close to identical. In each, only set the class’s attribute if the argument is an acceptable value (eg day must be between 1 and 31, month between 1 and 12, etc). If the argument isn’t acceptable, output an error message to the user.
Each of the getter functions will just return the raw value for the corresponding class attribute. They should be virtually identical.
The str function should return a string in a standard date format (your choice of format as long as it includes year, day, and month). Don’t include newline characters. Use the getter functions to get the values rather than outputting the variables directly. Make sure that the month and date get outputted with two digits (eg if day=1, it should be display as 01)
The compDate function should return a -1 if other comes before itself, a 0 if the dates are the same, and a 1 if other comes after itself.
The class constructor should set each of the year, month, and date using the built in setter functions.
In int main, create two instances of the date class. With the first instance, use the constructor to set the instance’s attributes equal to a date of personal importance. With the second instance, use the class’s setter functions to set the instance’s attributes to your graduation date (you can make up a date if you don’t recall this offhand). Finally, output both dates and a statement about which date comes first.
Sample Output (note: your output will probably differ if you pick different dates or a date format)
Personal date: 1990-10-07
Graduation: 2016-12-20
I graduate after my personal date
This is the cpp file that was included of what the program should include:
Solution
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
class Date
{
private:
int year;
int month;
int day;
public:
Date(void);
Date(int y, int m, int d);
void setYear(int y);
void setMonth(int m);
void setDay(int d);
int getYear();
int getMonth();
int getDay();
string str();
int compDate(Date d);
};
Date::Date(void)
{
year=0;
month=0;
day=0;
}
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void Date::setYear(int y)
{
year=y;
}
void Date::setMonth(int m)
{
month=m;
}
void Date::setDay(int d)
{
day=d;
}
int Date::getYear()
{
return year;
}
int Date::getMonth()
{
return month;
}
int Date::getDay()
{
return day;
}
int Date::compDate(Date d)
{
if(year>d.year)
{
return -1;
}
else if(year<d.year)
{
return 1;
}
else if(month>d.month)
{
return -1;
}
else if(month<d.month)
{
return 1;
}
else if(day>d.day)
{
return -1;
}
else if(day<d.day)
{
return 1;
}
return 0;
}
string Date::str()
{
stringstream s;
string r;
s << getYear() << \'-\' << setw(2) << setfill(\'0\') << getMonth() << \'-\' << setw(2) << setfill(\'0\') << getDay();
s >> r;
return r;
}
int main()
{
Date d1(1990,10,7);
Date d2;
d2.setYear(2016);
d2.setDay(20);
d2.setMonth(12);
cout<<d1.str()<<endl;
cout<<d2.str()<<endl;
if(d1.compDate(d2)>0)
{
cout<<d1.str()<<\" comes first.\"<<endl;
}
else if(d1.compDate(d2)<0)
{
cout<<d2.str()<<\" comes first.\"<<endl;
}
else
{
cout<<\"Both are same date.\"<<endl;
}
return 0;
}


