design a class which sotres month, date and year

Mar 28, 2011 at 9:39pm
"Design a class called Date that has integer data members to store month, day, and year.
The class should have a three-parameter default constructor that allows the date to be set at the time a new Date object is created. If the user creates a Date object without passing any arguments, or if any of the values passed are invalid, the default values of 1, 1, 2001 (i.e, January 1, 2001) should be used. The class should have member functions to print the date in the following formats:

3/15/2008
March 15, 2008
15 March 2008

Demonstrate the class by writing a program that uses it.

Input Validation: Only accept values between 1 and 12 for the month. Only accept values between 1 and 31 for the day. Only accept values between 1900 and 2010 for the year.

This is what i have so far but it is Giving me some errors please help me

#include <iostream>
using namespace std;


class date

{
int month;
int day;
int year;

public:

date (int month = 1, int day = 1, int year = 2001)

{

date::month = month;
date::day = day;
date::year = year;

};

void showDate();

~date(){}

};



void date::showDate(){

cout << month << "/" << day << "/" << year << endl;

}

int main()

{

int month;

int day;

int year;


string monthName[12] = {"January","February","March","April","May","June","July",

"August","September","October","November","December"};



cout << "enter month (between 1 and 12)" << endl;

cin >> month;



if (month > 12 || month < 1)

{

month = 1;

}

cout << "enter day (between 1 and 31)" << endl;

cin >> day;


if (day > 31 || day < 1)

{

day = 1;

}

cout << "enter year (between 1900 and 2010)"<< endl;

cin >> year;

if (year > 2010 || year < 1900)

{

year = 2001;

}


date newDate(month, day, year);
newDate.showDate();
cout << monthName[month-1] << " " << day << ", " << year << endl;
cout << day << " " << monthName[month-1] << " " << year << endl;


system ("PAUSE");

return 0;

}


Last edited on Mar 28, 2011 at 9:41pm
Mar 29, 2011 at 12:14am
You code does compile without errors here.

For the print member function you could use something as simple as that :

1
2
3
4
5
6
7
8
9
10
11
12
13
14

void date::showDate( std::string (&monthName)[12] )
{
	std::cout << month << "/" << day << "/" << year << std::endl;
	std::cout << monthName[month-1] << " " << day << ", " << year << std::endl;
	std::cout << day << " " << monthName[month-1] << " " << year << std::endl;
}

int maint()
{
	//...
	newDate.showDate( monthName );
	//...
}


Or you could store the montName string array into the date class instead.
Last edited on Mar 29, 2011 at 12:15am
Topic archived. No new replies allowed.