GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Dec-2009, 20:49
shyrenee89 shyrenee89 is offline
New Member
 
Join Date: Nov 2009
Posts: 1
shyrenee89 is on a distinguished road

Write a program that implements a class called Date


I have to write a program that implements a class called Date. i have the functions needed for it and the loops but I'm not sure how the main looks. Can someone please help with the main function.

Just in case you need to see the Assignment. its below;

Overview

For this assignment, you are to implement a class called Date. This class will be used to represent simple dates.
Date class

The following definition for the Date class should be placed at the top of the CPP file:

CPP / C++ / C Code:
class Date
{
public:
  Date( int, int, int );

  void setDate( int, int, int );

  void printToday();

  int daysPassed();
  int daysLeft();

private:
  int month, day, year;

  static const int monthDays[13];
  static const char * const monthNames[13];
};

Data Members

The Date class contains five data members that are not available to the outside world. They are:

* an integer to hold the month number
* an integer to hold the day number
* an integer to hold the year number
* an array of integer constants that represent the number of days in each month
* an array of constant pointers to character constants that represent the names of each of the months in a year

The two arrays both hold constant values but they cannot be initialized in the Date class definition because the definition is simply a pattern for what an object will "look like" when it is created. This means that there is no memory allocated for the class definition and therefore the two arrays must be initialized outside of the class definition. Place the following array declaration/initialization statements at the top of the CPP file after the closing curly brace for the class definition.

CPP / C++ / C Code:
const int Date::monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

const char * const Date::monthNames[13] = { "", "January", "February", "March", "April", "May",
                                      "June", "July", "August", "September", "October",
                                      "November", "December" };

Note 1: The arrays hold thirteen thirteen items sp that the month number can be used as a direct subscript to access a value. For example, to access the number of days in March use the statement monthDays[3].

Note 2: The class disregards leap years.
Constructor: Date( int monthVal, int dayVal, int yearVal )

The constructor for this class is used to initialize a Date object to a specific date. It takes three arguments: the initial month, the initial day, and the initial year. The passed in values should only be used if they are valid. The month value should be between 1 and 12, inclusive. If it is invalid, set the month to January. The day value should be between 1 and the maximum number of days in the current month, inclusive. If it is invalid, set the number of days to the maximum for the current month. The year value should be positive. If it is invalid, set the year to 2009.

Note/Hint: think about calling the setDate method that is described next.
Methods
void setDate ( int monthVal, int dayVal, int yearVal )

This method sets the date for the current object. It takes three arguments: the value for the month, the value for the day, and the value for the year. The passed in values should only be used if they are valid. The month value should be between 1 and 12, inclusive. If it is invalid, set the month to January. The day value should be between 1 and the maximum number of days in the current month, inclusive. If it is invalid, set the number of days to the maximum for the current month. The year value should be positive. If it is invalid, set the year to 2009.
void printToday()

This method prints the current date in the Date object in the format "monthName day, year". For example, if the month value is 11, day is 30, and year is 1977, then this method will print "November 30, 1977".
int daysPassed()

This method calculates and returns the number of days that have passed from the beginning of the year up to and including the current date in the Date object.

Hint: To get started, think about writing a loop that will add up the maximum number of days in each month up to the current month in the Date object.
int daysLeft()

This method calculates and returns the number of days that are left in the current year in the Date object. This will only count the days after the current date in the Date object.

Hint: To get started, think about writing a loop that will add up the maximum number of days in each month AFTER the current month in the Date object through December.
main

This part of the assignment is going to use the Date class described above. Create four Date objects with the values:

* November 13, 1975 for the first object
* January 1, 2009 for the second object
* December 31, 2005 for the third object
* Your birth date for the fourth object

For each of the objects, display the following:

* the current date
* the number of days that have passed in the current year
* the number of days left in the current year

Output

The output for this assignment should resemble the following:

Today is November 13, 1975
317 days have passed in the year
There are 48 days left in the year

Today is January 1, 2009
1 days have passed in the year
There are 364 days left in the year
Last edited by admin : 06-Dec-2009 at 17:09. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 04-Dec-2009, 23:49
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 450
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Classes


Perhaps something like this should do it?

CPP / C++ / C Code:
const int NUMBER_OF_DATES = 4;

int main()
{
    Date dates[NUMBER_OF_DATES];
    dates[0].setDate(/* the required arguments */);
    dates[1].setDate(/* the required arguments */);
    dates[2].setDate(/* the required arguments */);
    dates[3].setDate(/* the required arguments */);

    for (int i = 0; i < NUMBER_OF_DATES; i++)
    {
        // call printToday(), daysPassed(), daysLeft() for the i'th object
    }

    return 0;
}
  #3  
Old 05-Dec-2009, 01:58
zalezog zalezog is offline
Junior Member
 
Join Date: Oct 2007
Posts: 54
zalezog has a spectacular aura aboutzalezog has a spectacular aura about

Re: Classes


This thread has a similar question. But the specifications here seems to be pretty descriptive. (its almost the same)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes referencing one another kuroi C++ Forum 9 09-Oct-2008 02:10
Trouble with Classes Within Classes jdbrine C++ Forum 2 29-Jun-2006 15:26
Re: Derived FLTK Classes cable_guy_67 FLTK Forum 0 26-Jun-2005 20:07
Assistance with classes... Bravebird C++ Forum 7 27-Apr-2005 13:17
Fairly simple classes help please sammacs C++ Forum 0 30-Nov-2004 09:58

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:33.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.