Problem Description
Create a Date class with the following capabilities:
a) Output the date in multiple formats such as
MM/DD/YYYY
June 14, 1992
b) Use overloaded constructors to create Date objects initialized with dates of the formats in part a).
You should only have 3 member data: integer month, integer day, and integer year. Also, you should have at least 2 constructors and 2 functions in your Date.cpp.
Code:
Date::Date ( int mm, int dd, int yy )
{ // check mm, dd, and yy and save them into month, day, and year member data.
…
}
Date::Date ( string mstr, int dd, int yy )
{ // check and convert mstr, dd, and yy and save them
// into month, day, and year member data.
…
}
void Date::print ( )
{ // print the date in MM/DD/YYYY
…
}
void Date::printFullDate ( )
{ // print the date in long date such as
// June 24, 2002
…
}
You also need to create 3 get functions and 3 set functions for the member data
---------------------------------------------
This is what i have so far
Date.h
Code:
#ifndef DateFormat
#define DateFormat
#include <time.h>
#include <string.h>
class Date {
public:
Date();
Date( int, int );
Date( int, int, int );
Date( char *, int, int );
void setMonth( int );
void setDay( int );
void setYear( int );
void printDateSlash( void ) const;
void printDateMonth( void ) const;
void printDateDay( void ) const;
const char *monthName( void ) const;
int leapYear( void ) const;
int daysOfMonth( void ) const;
void convert1( int );
int convert2( void ) const;
void convert3( const char * const );
const char *monthList( int ) const;
int days( int ) const;
private:
int day;
int month;
int year;
};
#endif
Date.cpp
Code:
#include <iostream.h>
#include <string.h>
#include <time.h>
#include "Date.h"
Date::Date()
{
struct tm *ptr;
time_t t = time( 0 );
ptr = localtime( &t );
day = ptr->tm_mday;
month = 1 + ptr->tm_mon;
year = ptr->tm_year + 1900;
}
Date::Date( int ddd, int yyyy )
{
setYear( yyyy );
convert1( ddd );
}
Date::Date( int mm, int dd, int yy )
{
setYear( yy + 1900 );
setMonth( mm );
setDay( dd );
}
Date::Date( char *mPtr, int dd, int yyyy )
{
setYear( yyyy );
convert3( mPtr );
setDay( dd );
}
void Date::setDay( int d )
{ day = d >= 1 && d <= daysOfMonth() ? d : 1; }
void Date::setMonth( int m ) { month = m >= 1 && m <= 12 ? m : 1; }
void Date::setYear( int y ) { year = y >= 1900 && y <= 1999 ? y : 1900; }
void Date::printDateSlash( void ) const
{ cout << month << '/' << day << '/' << year << '\n'; }
void Date::printDateMonth( void ) const
{ cout << monthName() << ' ' << day << ", " << year << '\n'; }
void Date::printDateDay( void ) const
{ cout << convert2() << ' ' << year << '\n'; }
const char *Date::monthName( void ) const { return monthList( month - 1 ); }
int Date::daysOfMonth( void ) const
{ return leapYear() && month == 2 ? 29 : days( month ); }
int Date::leapYear( void ) const
{
if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
return 1;
else
return 0;
}
void Date::convert1( int ddd )
{
int dayTotal = 0;
if ( ddd < 1 || ddd > 366 )
ddd = 1;
setMonth( 1 );
for ( int m = 1; m < 13 && ( dayTotal + daysOfMonth() ) < ddd; ++m ) {
dayTotal += daysOfMonth();
setMonth( m + 1 );
}
setDay( ddd - dayTotal );
setMonth( m );
}
int Date::convert2( void ) const
{
int ddd = 0;
for ( int m = 1; m < month; ++m )
ddd += days( m );
ddd += day;
return ddd;
}
void Date::convert3( const char * const mPtr )
{
int flag = 0;
for ( int subscript = 0; subscript < 12; ++subscript )
if ( !strcmp( mPtr, monthList( subscript ) ) ) {
setMonth( subscript + 1 );
flag = 1;
break;
}
if ( !flag )
setMonth( 1 );
}
const char *Date::monthList( int mm ) const
{
char *months[] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" };
return months[ mm ];
}
int Date::days( int m ) const
{
const int monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
return monthDays[ m - 1 ];
}
int main()
{
Date d1( 7, 4, 98 ), d2( 86, 1999 ),
d3, d4( "September", 1, 1998 );
d1.printDateSlash();
d2.printDateSlash();
d3.printDateSlash();
d4.printDateSlash();
cout << '\n';
d1.printDateDay();
d2.printDateDay();
d3.printDateDay();
d4.printDateDay();
cout << '\n';
d1.printDateMonth();
d2.printDateMonth();
d3.printDateMonth();
d4.printDateMonth();
cout << endl;
return 0;
}
Error codes :
---32:2 C:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
---- C:\Users\jowner\Desktop\Date.cpp In member function `void Date::convert1(int)':
----94 C:\Users\jowner\Desktop\Date.cpp name lookup of `m' changed for new ISO `for' scoping
----88 C:\Users\jowner\Desktop\Date.cpp using obsolete binding at `m'
Please help, i cant get it to work