Complete the program date.cpp which uses the class date to represent dates writt
ID: 3753378 • Letter: C
Question
Complete the program date.cpp which uses the class date to represent dates written in the
form day/month/year. Do not modify the main program.
You must write the prototypes and the denitions of the methods of the class. Keep the
following rules in mind:
Rule regarding pass by reference:
When deciding how to pass function arguments:
if the argument must be changed by the function {
pass by reference
}
else if the argument is an object {
pass by reference but declare the object to be const
}
else{
pass by value
}
Rule regarding const methods:
If a method does not change the object then the keyword const is placed after the function.
/* File: date.cpp
A class representing dates in the form: day, month and year
dates are written to a stream in the form day/month/year
Assignment 2 6
day_number() returns the number of days since 1/1 of the current year
including the current day
#include <iostream>
#include <fstream>
using namespace std;
class date {
private:
int day;
int month;
int year;
public:
// you fill in the method prototypes
};
// number of days in each month
const int DAYS[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int main(void)
{
ofstream fout("date.out");
date d(12, 6, 2010);
date e(19, 9, 2013);
fout << "For the date is ";
d.write(fout);
fout << endl;
fout << "Day number is " << d.day_number() << endl;
fout << " For the date is ";
e.write(fout);
fout << endl;
fout << "Day number is " << e.day_number() << endl;
return 0;
}
///////////////////////// Methods of date ////////////////////////////
/////////////////////////////////////////////////////////////////////////////
Your program output should be:
For the date is 12/6/2010
Day number is 163
For the date is 19/9/2013
Day number is 262
Explanation / Answer
/* File: date.cpp
A class representing dates in the form: day, month and year
dates are written to a stream in the form day/month/year
Assignment 2 6
day_number() returns the number of days since 1/1 of the current year
including the current day
*/
#include <iostream>
#include <fstream>
using namespace std;
class date {
private:
int day;
int month;
int year;
public:
// Parameterized Constructor
date(int Day, int Month, int Year)
{
day = Day;
month = Month;
year = Year;
}
void write(std::ofstream& fout)
{
fout << day << "/" << month << "/" << year;
}
// number of days in each month
const int DAYS[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
// method to calculate day number
int day_number()
{
int startDay = 1;
int startMonth = 1;
int totalDays = 0;
for(int i = startMonth; i < month; i++)
{
for(int j = startDay; j <= DAYS[i-1]; j++)
{
totalDays += j;
}
}
totalDays += day;
return totalDays;
}
};
int main(void)
{
ofstream fout("date.out");
date d(12, 6, 2010);
date e(19, 9, 2013);
fout << "For the date is ";
d.write(fout);
fout << endl;
fout << "Day number is " << d.day_number() << endl;
fout << " For the date is ";
e.write(fout);
fout << endl;
fout << "Day number is " << e.day_number() << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.