C++ multi file classes

closed account (2EURX9L8)
I have an assignment and have got most of the first part do. There is a part however, I am having troubles with..I am not sure what she is getting across for the part asking to make a Date::setDate().....not sure what to put in it...

any way here is the long ass assignment

The Date class

This class has three member variables

month – an integer in the range of 1-12
day – an integer within the range of the allowed month
year – an integer in the range of 0-3000

This class has the following member functions. These are the actual function prototypes. You are to write the function implementation for each function.

Date();
This is the default constructor. It is to set day to 1, month to 1, and year to 2000.

Date(int,int,int=0);
This is another constructor. The parameters are in the following order: month, day, year. Setting the year to 0 identifies that this date does not need a year.

int getDay();
This is the get function for day.

int getMonth();
This is the get function for month.

int getYear();
This is the get function for year.

void setDay(int);
This is the set function for day. It is to verify the day sent is within the correct range for the current month. If it is, set it to the sent value. If it is not within the correct range for the month, set it to 1. I have provided the function implementation for you. You may use it if you would like. Click here to get the file.

void setMonth(int);
This is the set function for month. It is to verify the month sent is between 1-12. If it is, set it to the sent value. If it is not within the correct range for the month, set it to 1.

void setYear(int);
This is the set function for year. It is to verify the year sent is between 0-3000. If it is, set it to the sent value. If it is not within the correct range for the year, set it to 0.

void setDate(int, int, int=0);
This is a set function for an entire date. It is to call the appropriate set functions for month, day, and year. It is important that you set the month and the year before setting the day. Setting the year to 0 identifies that this date does not need a year.

void printShort(ostream&);
This function is to print the Date using the following format: month/day/year
Example: 3/28/2009
If the year is 0, print it using the following format:
month/day
Example: 12/25
It is to print the Date to the output stream sent to the function.

void printLong(ostream&);
This function is to print the Date using the following format:
MonthName day, year
Example: March 28, 2009
If the year is 0, print it using the following format:
MonthName day
Example: December 25
It is to print the Date to the output stream sent to the function.
The Holiday class

This class has two member variables

date – a Date that contains the date of the holiday
name – a string that contains the name of the holiday

This class has the following member functions. These are the actual function prototypes. You are to write the function implementation for each function.

Holiday(string,int,int,int=0);
This is a constructor. The parameters are in the order name, month, day, year.

Holiday(string, Date);
This is a constructor. The parameters are in the order name, date.

void print(ostream&);
This function prints out the Holiday using the following format:
name monthName day, year.
Example: Thanksgiving November 24, 2011
if year is 0 the function is to print the Holiday using the following format:
name monthName day.
Example: Christmas December 25
It is to print the Date to the output stream sent to the function.

void setDate(int,int,int=0);
This function calls the correct Date function that sets the date member variable to the values sent.

void setName(string);
This is the set function for name.

Date getDate();
This is accessor function for date.

string getName();
This is the accessor function for name.

The file testMain.cpp is an application that tests each function in the Date and Holiday classes. Add this file to your project then code the classes. I suggest you write the member functions in the order listed below. Test each member function as you complete it. The code in main is all commented out. To test each function, move the opening comment tag (/*) down to the next section.



I was thinking something like this...
1
2
3
4
5
void Date::setDate(int m, int d, int y){
	Date.setMonth(m)=month;

	setYear(y);
	setDay(d);


not sure what she wants and I know I am doing it wrong....Anyway if you got through all that, any help would be greatly appreciated.
Last edited on
You pretty much have the idea with setYear and setDay. Do the same thing with setMonth.

(Suggestion: have the constructor call setDate, to avoid code duplication)
closed account (2EURX9L8)
and the year before setting the day. Setting the year to 0 identifies that this date does not need a year.

Not getting that part

and as for the suggestion is this what you are talking about?

1
2
3
4
5
6
7
8
Date::Date(int m, int aDay, int y){
	 
	
	
	Date.setMonth(m);
	Date.setDay(aDay);
	Date.setYear(y);
}
Last edited on
closed account (2EURX9L8)
What about the constructor on the second part. Do you guys think I need to call the Date class and put it in the constructor for the Holiday class? kinda like this?



Holiday(string,int,int,int=0);
This is a constructor. The parameters are in the order name, month, day, year.

1
2
3
4
5
6
7
8
9
10
Holiday::Holiday(string n, int m, int d, int y){
    date=d;
	name=n;
	Date::setMonth(m);
	Date::setDay(d);
	Date::setYear(y);



}
Last edited on
I think you're making it a touch more difficult than it needs to be. Don't forget, your Date class has a SetDate method. You can just as easily use that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Class
class Holiday
{
private:
   Date date;
   string name;
public:
   Holiday(string, int, int, int);
};

// Constructor
Holiday::Holiday(string n, int m, int d, int y)
{
   name = n;
   date.SetDate(m,d,y);
}


The only think I can think of for the zero identifying that the date does not need a year is for when you implement holiday dates.

For example, Christmas would be set up like so:
 
Holiday Christmas("Christmas", 12, 25, 0);

Because Christmas falls on the same date each year.
Last edited on
closed account (2EURX9L8)
Thanks iHutch105 that helpt a lot
Last edited on
closed account (2EURX9L8)
Please
Last edited on
closed account (2EURX9L8)
Did


Last edited on
Topic archived. No new replies allowed.