Assignment: Your task is to write a C++ program to create and test a class calle
ID: 3787615 • Letter: A
Question
Assignment:
Your task is to write a C++ program to create and test a class called Date that represents a date. Step 1 - Define the Class. Create a new project and enter the following Date class definition, in a header (.h) file: Description of Date class: Data members: o An unsigned month to hold the month of the date o An unsigned day to hold the day of the date o An unsigned year to hold the year of the date For example, July 4, 1776, is month 7, day 4 and year 1776 Function members: o A default constructor which initializes the date to January 1st, 1 o an explicit constructor which initializes the object to a Date with given month, day and year. receives: a month, a day and a year o a mutator(set operation) SetYear( ) which sets the date with given month, day and year. receives: a month, a day and a year o an accessor(get operation) GetYear( ) returns the date’s year o an accessor(get operation) GetMonth( ) returns the date’s month o an accessor(get operation) GetDay( ) returns the date’s day o overloaded < operator receives another Date object D returns: true if and only if the parameter Date D is after the date itself, otherwise false o overloaded << operator(friend function): output date as format mm/dd/year o overloaded input operator (friend function) Step 2 - Write the Member and Friend Functions. In an implementation (.cpp) file, write the code to implement the member and friend functions defined in the class. Step 3 - Write the main function. Write a test program to test all the above functions. Create two Date objects(yearOne, yearTwo), yearOne with default constructor and yearTwo with the explicit constructor Output the above two Date objects Re-set yearOne to a new date and output the new date’s year Compare yearOne and yearTwo, and output the greater one Create a third Date object dateThree with default constructor, then input new month, day and year values to dateThree with overloaded input operator and display it with overloaded output operator
I have this so far for my header file:
#include <iostream>
using namespace std;
class Date
{
public:
Date();
void date(unsigned month, unsigned day, unsigned year);
void SetYear();
unsigned GetYear();
unsigned GetMonth();
unsigned GetDay();
Date(unsigned);
friend ostream & operator<<(ostream & out, const Date & c);
friend istream & operator >> (istream & in, Date & c);
private:
unsigned month;
unsigned day;
unsigned year;
};
Here is what I have so far for my Date.cpp:
#include <iostream>
#include <string>
#include "Date.h"
using namespace std;
Date::Date()
{
month = 1;
day = 1;
year = 1;
}
void Date::date(unsigned m, unsigned d, unsigned y)
{
month = m;
day = d;
year = y;
}
void SetYear()
{
}
unsigned Date::GetYear()
{
return year;
}
unsigned Date::GetMonth()
{
return month;
}
unsigned Date::GetDay()
{
return day;
}
Date::Date(unsigned)
{
}
ostream & operator<<(ostream & out, const Date & c)
{
}
istream & operator >> (istream & in, Date & c)
{
}
Explanation / Answer
// Date.h
#include <iostream>
using namespace std;
class Date
{
public:
Date();
Date(unsigned month, unsigned day, unsigned year);
void SetYear(unsigned y);
void SetMonth(unsigned m);
void SetDay(unsigned d);
unsigned GetYear();
unsigned GetMonth();
unsigned GetDay();
bool operator <(const Date &D);
friend ostream & operator<<(ostream & out, const Date & c);
friend istream & operator >> (istream & in, Date & c);
private:
unsigned month;
unsigned day;
unsigned year;
};
// Date.cpp
#include <iostream>
#include <string>
#include "Date.h"
using namespace std;
Date::Date()
{
month = 1;
day = 1;
year = 1;
}
Date::Date(unsigned m, unsigned d, unsigned y)
{
month = m;
day = d;
year = y;
}
void Date::SetYear(unsigned y)
{
year = y;
}
void Date::SetMonth(unsigned m)
{
month = m;
}
void Date::SetDay(unsigned d)
{
day = d;
}
unsigned Date::GetYear()
{
return year;
}
unsigned Date::GetMonth()
{
return month;
}
unsigned Date::GetDay()
{
return day;
}
bool Date::operator <(const Date &D) {
if(year < D.year) return true;
else if(year > D.year) return false;
else{
if(month < D.month) return true;
else if(month > D.month) return false;
else{
if(day < D.day) return true;
else return false;
}
}
}
ostream & operator<<(ostream & out, const Date & c)
{
out << c.month << "/" << c.day << "/" << c.year << " ";
return out;
}
istream & operator >> (istream & in, Date & c)
{
in >> c.month >> c.day >> c.year;
}
// main.cpp
#include <iostream>
#include "Date.cpp"
using namespace std;
int main(){
unsigned m = 12, d = 31, y = 2001;
Date yearOne;
Date yearTwo(m, d, y);
cout << yearOne;
cout << yearTwo;
yearOne.SetMonth(10);
yearOne.SetDay(12);
yearOne.SetYear(2004);
cout << yearOne;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.