Thread: date class

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    date class

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Date {
    public:
    	Date(int day = 1,int month = 1, int year = 2000);
    	void setDay(int day);
    	void setMonth(int month);
    	void setYear(int year);
    	int getDay() const;
    	int getMonth() const;
    	void getYear(int& year) const; // return by reference
    	void increment(); // sets values to the next day
    	void print() const; // print in dayMonYear format
    private:
    	int thisDay;
    	int thisMonth;
    	int thisYear;
    	void checkDay(); // if day is out of range - reset to first of month
    	void checkMonth(); // if month is out of range - reset to january
    	static int daysInMonth[];
    	static string month[];
    
    };
    
    int main()
    {
    	Date notToday;
    
    
    	cout << "notToday: ";
    	notToday.print();
    	notToday.increment();
    	cout << "\nnotToday after increment: ";
    	notToday.print();
    
    
    	// have user type in day/month/year - used to testing increment
    	int day, month, year;
    	cout << "\nenter date (day month year), e.g. 31 1 2000: ";
    	cin >> day >> month >> year;
    	Date someDay(day,month,year);
    	year = 0; // just to show change
    	someDay.getYear(year); // becuase return by reference
    	cout << "Day: " << someDay.getDay() << " month: " << someDay.getMonth() << " year:" << year;
    	cout << "\nprinting: ";
    	someDay.print();
    	someDay.increment();
    	cout << "\nafter increment: ";
    	someDay.print();
    	cout << "\nenter date (day month year), e.g. 31 1 2000: ";
    	cin >> day >> month >> year;
    	someDay.setDay(day);
    	someDay.setMonth(month);
    	someDay.setYear(year);
    	cout << "\nprinting: ";
    	someDay.print();
    	someDay.increment();
    	cout << "\nafter increment: ";
    	someDay.print();
    	
    
    	return 0;
    }
    
    int Date::daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
    string Date::month[]={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
    
    Date::Date(int day,int month, int year)
    {
    	thisDay = day;
    	thisMonth = month;
    	thisYear = year;
    	checkMonth();
    	checkDay(); // why check day second?
    }
    
    void Date::setDay(int day)
    {
    	thisDay = day;
    	checkDay();
    }
    void Date::setMonth(int month)
    {
    	thisMonth = month;
    	checkMonth();
    	checkDay(); // may become out of range due to change in month
    }
    void Date::setYear(int year)
    {
    	thisYear = year;
    }
    
    int Date::getDay() const
    {
    	return thisDay;
    }
    int Date::getMonth() const
    {
    	return thisMonth;
    }
    
    void Date::getYear(int& year) const// return by reference
    {
    	year = thisYear;
    }
    void Date::increment() // sets values to the next day
    {
    	
    	// first increment day
    	thisDay++;
    	if(thisDay > daysInMonth[thisMonth-1]) {
    		thisDay = 1; // first of the next month
    		thisMonth++;
    		if(thisMonth > 12) {
    			thisMonth = 1; // January next year
    			thisYear++;
    		}
    	}
    }
    
    void Date::print() const // print in dayMonYear format
    {
    	cout << thisDay << month[thisMonth-1] << thisYear;
    }
    
    void Date::checkMonth()
    {
    	if((thisMonth < 1) || (thisMonth > 12))
    		thisMonth = 1;
    }
    
    void Date::checkDay()
    {
    	if((thisDay < 1) || (thisDay > daysInMonth[thisMonth-1])) {
    		thisDay = 1;
    	}
    }
    Ok,I have some code that gives the next day after you give it a date. I need help trying write a function that I will call "occursBefore", for this class I want to take two "Date" objects and return a "-1" if the first date is chronologically before the second date, return "1" if the first date is chronologically after the second date and return "0" if the two dates are the same.
    And if someone feels really fresh, I am going to see if I can do a Days between, where it returns a integer for the number of days between.
    Any help would be greatly appreciated
    Aquaman

    // edited by da for code tags... use em buddy, they really help...

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You could have an internal representation of the date as total number of days within your class -

    TotalDays = years*365 + months*30 + days.

    It would be more complex than this though, as you'd have to take into account leap years and not every month has 30 days (but as you've already created your month/days table so this should be pretty easy to get around).
    zen

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    How about this operator for Date comparisons ?

    Code:
    bool Date::operator > (const Date& date) const
    {
    	if(m_Year>date.m_Year) return true;
    	if(m_Year<date.m_Year) return false;
    
    	if(m_Month>date.m_Month) return true;
    	if(m_Month<date.m_Month) return false;
    
    	if(m_Day>date.m_Day) return TRUE;
    
    	return false;
    }
    To apply the cout principles to your class try doing this:

    Code:
    #include <iostream.h>
    
    class Date
    {
    private:
    	//[...]
    
    public:
    	//[...]
    
    	friend ostream& operator << ( ostream& out, const Date& date );
    };
    
    ostream& operator << ( ostream& out, const Date& date )
    {
    	out << date.thisDay << " " << month[date.thisMonth-1] << " " << date.thisYear; 
    
    	return out;
    }
    
    int main()
    {
    	Date d;
    	
    	cout << "printing date: " << d << " looks neat, doesn't it ?";
    
    	return 0;
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    25
    Why did you do get year the way you did? I don't see the logic in just not returning a value from the member function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM