This code defines a Date class with various constructors and methods to work with dates. It includes header files for input/output streams, time functions, and the Date class definition. The main function creates some Date objects and calls printing methods to output the dates in different formats. The Date.cpp file implements the class methods, including constructors that initialize dates from components or the current system time, and methods to set/get date values and convert between formats.
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
Please make a comment for all the codesOr explain this pro.pdf
1. Please make a comment for all the codes
Or explain this program code!!
//Date.h
#ifndef DateFormat
#define DateFormat
#include
using std::string;
class Date {
public:
Date();
Date( int, int );
Date( int, int, int );
2. Date( string, int, int );
void setDay11(int);
void setMonth11( int );
void print11() const;
void printDDDYYYY11( ) const;
void printMMDDYY11( ) const;
void printMonthDDYYYY11( ) const;
~Date();
private:
int month1;
int day1;
int year1;
3. int checkDay(int)const;
int daysInMonth(int)const;
bool isLeapYear()const;
int convertDDToDDD()const;
void setMMDDFromDDD(int);
string convertMMToMonth(int)const;
void setMMFromMonth(string);
int convertYYYYToYY()const;
void setYYYYFromYY(int);
};
#endif
12. using std::string;
class Date {
public:
Date();
Date( int, int );
Date( int, int, int );
Date( string, int, int );
void setDay11(int);
void setMonth11( int );
void print11() const;
void printDDDYYYY11( ) const;
void printMMDDYY11( ) const;
13. void printMonthDDYYYY11( ) const;
~Date();
private:
int month1;
int day1;
int year1;
int checkDay(int)const;
int daysInMonth(int)const;
bool isLeapYear()const;
int convertDDToDDD()const;
void setMMDDFromDDD(int);
string convertMMToMonth(int)const;
22. }
Please make a comment for all the codes
Or explain this program code!!
//Date.h
#ifndef DateFormat
#define DateFormat
#include
using std::string;
class Date {
public:
Date();
Date( int, int );
Date( int, int, int );
23. Date( string, int, int );
void setDay11(int);
void setMonth11( int );
void print11() const;
void printDDDYYYY11( ) const;
void printMMDDYY11( ) const;
void printMonthDDYYYY11( ) const;
~Date();
private:
int month1;
int day1;
int year1;
24. int checkDay(int)const;
int daysInMonth(int)const;
bool isLeapYear()const;
int convertDDToDDD()const;
void setMMDDFromDDD(int);
string convertMMToMonth(int)const;
void setMMFromMonth(string);
int convertYYYYToYY()const;
void setYYYYFromYY(int);
};
#endif
//Main.cpp
25. include
using std::cout;
using std::endl;
#include "Date.h"
int main()
{
Date date1( 252,1999 );
Date date2( 2,25,04 );
Date date3( "September", 1, 2000 );
Date date4;
date1.print11();
date2.print11();
29. time_t t1 = time( 0 );
ptr = localtime( &t1 );
day1 = ptr->tm_mday;
month1 = 1 + ptr->tm_mon;
year1 = ptr->tm_year + 1900;
}
Date::Date( int ddd1, int yyyy1 )
{
year1=yyyy1;
setMMDDFromDDD(ddd1);
}
Date::Date( int mm1, int dd1, int yy1 )
30. {
setYYYYFromYY( yy1 );
include
using std::cout;
using std::endl;
#include "Date.h"
int main()
{
Date date1( 252,1999 );
Date date2( 2,25,04 );
Date date3( "September", 1, 2000 );
Date date4;
date1.print11();
35. Date::Date( int mm1, int dd1, int yy1 )
{
setYYYYFromYY( yy1 );
include
using std::time;
using std::localtime;
using std::tm;
using std::time_t;
#include "Date.h"
Date::Date()
{
struct tm *ptr;
36. time_t t1 = time( 0 );
ptr = localtime( &t1 );
day1 = ptr->tm_mday;
month1 = 1 + ptr->tm_mon;
year1 = ptr->tm_year + 1900;
}
Date::Date( int ddd1, int yyyy1 )
{
year1=yyyy1;
setMMDDFromDDD(ddd1);
}
Date::Date( int mm1, int dd1, int yy1 )
42. {
year1=(yy<7?yy+2000:yy+1900);
}
Solution
//Date.h
#ifndef DateFormat
#define DateFormat
#include
using std::string;
class Date {
public:
// constructor
Date();
// parameterized consturctors
Date( int, int );
Date( int, int, int );
Date( string, int, int );
// setters
void setDay11(int);
void setMonth11( int );
// print functions
void print11() const;
void printDDDYYYY11( ) const;
void printMMDDYY11( ) const;
void printMonthDDYYYY11( ) const;
~Date();
private:
// variables
43. int month1;
int day1;
int year1;
// check valid day
int checkDay(int)const;
// count days in month
int daysInMonth(int)const;
// check leap year
bool isLeapYear()const;
// converter function
int convertDDToDDD()const;
// setter
void setMMDDFromDDD(int);
// converter function
string convertMMToMonth(int)const;
// setter
void setMMFromMonth(string);
// converter function
int convertYYYYToYY()const;
// setter
void setYYYYFromYY(int);
};
#endif
//Date.cpp
// input output library
#include
using std::cout;
using std::endl;
// library to set width and fill
#include
using std::setw;
using std::setfill;
// library to use time
#include
using std::time;
using std::localtime;
44. using std::tm;
using std::time_t;
#include "Date.h"
// default constructor
Date::Date()
{
// taking pointer to tm
struct tm *ptr;
// set time as 0
time_t t1 = time( 0 );
// pointer taking address of t1
ptr = localtime( &t1 );
// day1 holds the day value of ptr
day1 = ptr->tm_mday;
// month1 holds the month value of ptr
month1 = 1 + ptr->tm_mon;
// year1 holds the year value of ptr + 1900
year1 = ptr->tm_year + 1900;
}
// parameterized constructor
Date::Date( int ddd1, int yyyy1 )
{
// set year
year1=yyyy1;
// call the month from date function
setMMDDFromDDD(ddd1);
}
// parameterized constructor
Date::Date( int mm1, int dd1, int yy1 )
{
// call the year from year function
setYYYYFromYY( yy1 );
// set day function
setDay11( dd1 );
}
// parameterized constructor
45. Date::Date(string monthName2,int dd1,int yyyy1)
{
// call the month from month function
setMMFromMonth(monthName2);
// setday
setDay11(dd1);
year1=yyyy1;
}
void Date::setDay11( int d1 )
{
// check for valid day and set it
day1 = checkDay(d1);
}
void Date::setMonth11( int m1 )
{
// check for valid month and set it
if(m1>0&& m1<=12)
month1=m1;
else
{
// end set month to 1
month1=1;
cout<<"invalid month("<0 && testDay<=daysInMonth(month1))
return testDay;
if(month1==2 && testDay==29 && isLeapYear())
return testDay;
cout<<"invalid day("<=2000?year1-2000:year1-1900);
}
// other yer converter function
void Date::setYYYYFromYY(int yy)
{
year1=(yy<7?yy+2000:yy+1900);
}
//Main.cpp
// input output library
46. #include
using std::cout;
using std::endl;
#include "Date.h"
int main()
{
// calling parameterized constructors
Date date1( 252,1999 );
Date date2( 2,25,04 );
Date date3( "September", 1, 2000 );
// print dates
Date date4;
date1.print11();
date2.print11();
date3.print11();
date4.print11();
// print date to years
cout << ' ';
date1.printDDDYYYY11();
date2.printDDDYYYY11();
date3.printDDDYYYY11();
date4.printDDDYYYY11();
// print month to date to years
cout << ' ';
date1.printMMDDYY11();
date2.printMMDDYY11();
date3.printMMDDYY11();
date4.printMMDDYY11();
// print final dates
cout << endl;
date1.printMonthDDYYYY11();
date2.printMonthDDYYYY11();
date3.printMonthDDYYYY11();
date4.printMonthDDYYYY11();
return 0;
}
47. /*
output:
invalid day(193)set to 1
3/1/1999
3/25/2004
9/1/2000
10/11/2016
60 1999
85 2004
245 2000
285 2016
03/0199
03/2504
09/0100
10/1116
March 1,1999
March 25,2004
September 1,2000
October 11,2016
date object destructor for date10/11/2016
date object destructor for date9/1/2000
date object destructor for date3/25/2004
date object destructor for date3/1/1999
*/