The Date Class

I am using a low level of complexity to show the working of general C++ classes, operator overloading and data hiding/encapsulation concepts. Below is the header file of Date Class. complete implementation will follow. However, Complete code of Date class can be used freely.

Questions/comments welcomed..!

Header File: date.h

#ifndef DATE_H
#define DATE_H

#include <iostream>

class Date
{
private:
short day;
short month;
short year;
short daysOfMonth(Date d);    /**returns the no of days in a month*/
static const short daysInMonth[]; /** array containing the 12 month’s days*/
bool leapYear (short);        /** tells the year is leap year or not*/

public:
/**constructor with default arguments*/
Date(short d=1, short m = 1, short y = 1900);

void display (); /** Display the date on the screen*/
void setDate (short, short, short); /** set the date with given arguments*/
Date operator ++(); /**pre increment operator used as ++date1*/
Date operator +(short); /**plus operator used as date1 + 5 */
Date operator –(); /**pre decrement operator used as –date1 */
Date operator -(short); /**decrement operator used as date1 – 5 */
short operator – (Date); /** return number of days between two dates */
Date operator +=(short); /** add short in left hand operand and return the same. */
Date operator -=(short); /** decrement short in left hand operand and return the same. */
short operator -=(Date); /** decrement Date object in left hand operand and return the same. */
friend Date operator – (short, Date); /**decrement operator used as 5 – date1 */

/**logical comarison operators*/
bool operator < (Date);
bool operator > (Date);
bool operator <=(Date);
bool operator >= (Date);
bool operator == (Date);
bool operator != (Date);

/** Stream insertion operators to get input and output with simple, cin and cout. */
friend std::ostream& operator << (std::ostream&, Date&);
friend std::istream& operator >> (std::istream&, Date&);

};

#endif // DATE_H

Advertisements

About Khuram Ali

Programming... Programming and Programming...!!!

Posted on March 21, 2013, in A library re-written., C++, Programming and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: