-
Help with Date Class
I'm trying to create a Date Class to run with some predetermined input and I am having a tought time with a few areas.
I haven't coded in C or C++ for awhile so I have a feeling this code is going to look really ugly to some people, so any other suggestions to clean it up are more than welcome.
Basically I'm not sure how I should get the getMonthName() function to work to print out the name of the month. I believe the way I declared it in my class header and member-function is incorrect.
All help is appreciated.
Code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Date
{
private:
int month;
int day;
int year;
public:
Date();
Date( int mn, int dy, int yr );
int getDay();
int getMonth();
int getYear();
void getMonthName();
void print() const;
void printLong() const;
};
Date::Date( int mn, int dy, int yr )
{
if ( mn > 0 && mn <= 12 )
month = mn;
else
month = 1;
if ( yr >= 1900 )
year = yr;
else
year = 1900;
static int length[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ( dy == length[month] )
day = dy;
else
day = 1;
}
int Date::getDay()
{
return day;
}
int Date::getMonth()
{
return month;
}
int Date::getYear()
{
return year;
}
void Date::getMonthName()
{
static char *name[] = { "nothing", "January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December" };
return name[month];
}
void Date::print() const
{
cout << month << day << year;
}
void Date::printLong() const
{
cout << day << month << year;
}
int main()
{
Date d1; // default ctor
Date d2(7, 4, 1976); // July 4'th 1976
Date d3(0, 15, 1880);// Adusted by ctor to January 15'th 1900
d1.print(); // prints 01/01/2000
d1.printLong(); // prints 1 January 2000
cout << endl;
d2.print(); // prints 07/04/1976
d2.printLong(); // prints 4 July 1976
cout << endl;
d3.print(); // prints 01/15/1900
d3.printLong(); // prints 15 January 1900
cout << endl;
cout << "object d2's day is " << d2.getDay() << endl;
cout << "object d2's month is " << d2.getMonth() << " which is " << d2.getMonthName() << endl;
cout << "object d2's year is " << d2.getYear() << endl;
}
-
If you want to have a "return" statement in your function, it sure as heck can't be void. Presumably you meant to have an array of strings and return something of string type.
-
I hate to say it but that date class just isn't useful. It is nothing more than a container for some values, and a formatter for formating them specifically to cout. Add to that the fact that its broken for leap years, (assuming you fix the compile errors), and it has very little real world value.
To simply "clean it up" really wont cut it. Study something like COleDateTime for an idea of some of the stuff that such a class needs to do to be useful.
I will show some ways of improving this function though:
Code:
std::string Date::getMonthName() const
{
static const char * const name[] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December", };
return name[month-1];
}
Even this function isn't such a good idea though, as it is obviously tied to just English. One can ask the operating system to format the date for you instead, and then it'll work for many languages.
Take the time to learn about const-correctness when you can.
-
this is the first program for an intro to c++ class im taking. what i have coded is what the professor has asked for so im only following by the rules i was given.
anyways, thank you for the help. i missed a couple other things too, but the month name is working correctly now. ill heed your advice in my future programming however.