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; }



LinkBack URL
About LinkBacks


