Monday, November 2, 2009

An example of a date class in C++

Tossing some of these up here for my own reference:

/*
Title: Date Class (header file) - prototype
Author: D. Tanner
class: CS 1415
Date: January 5th, 2006
*/

#ifndef TANNDATE_H
#define TANNDATE_H

#include
#include
#include
using namespace std;

//Constants
const int DAYS_PER_YEAR = 365;
const int DAYS_PER_LPYR = 366;
const int DAYS_PER_MON[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31};
const int DAYS_PER_MON_LPYR[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31};

//Date class function declarations
class TannDate
{
private:
int dayOfYear_;
int dayOfMonth_;
int month_;
int year_;
string monthName_;

//General Calculation Functions
//These functions must be called only under the circumstances that the
//corresponding data members have been given appropriate values
void calcDayOfYear();
void calcFromDayOfYear();
void calcMonthName();

//private set functions
void setDayOfYear(int dayOfYear){dayOfYear_ = dayOfYear;}
void setDayOfMonth(int dayOfMonth){dayOfMonth_ = dayOfMonth;}
void setMonth(int month){month_ = month;}
void setMonth(string monthName) {monthName_ = monthName;}
void setYear(int year){year_ = year;}

public:
//Exception class
class BadDate
{
private:
string errorMsg_;

public:
BadDate(string error){errorMsg_ = error;}
string getError(){return errorMsg_;}
};

//Get functions
int getDayOfYear(){return dayOfYear_;}
int getDayOfMonth(){return dayOfMonth_;}
int getMonth(){return month_;}
int getYear(){return year_;}
string getMonthName(){return monthName_;}

//set functions
void setDate(int month, int dayOfMonth, int year);
void setDate(int dayOfYear, int year);

//Constructors
TannDate();
TannDate(int month, int dayOfMonth, int year);
TannDate(int dayOfYear, int year);

//Print Functions
void printShortDate();
void printLongDate();
void printDOY();
};

#endif

No comments:

Post a Comment