I need help with Classes/structures!

Hi I am currently writing a code that finds the birthday of someone!

-The class should have a three parameter default constructor that allows the date to be set when a new Date object is created.
-If the user creates a Date object without any arguments or if the arguments have invalid values:
• month should be between 1 and 12
• date should be between 1 and the number of days in the selected month
then the default values should be 1,1,2001.
The class should have member functions that allow to print the date in any of the three following formats:
• 3/15/13
• March 15, 2013
• 15 March 2013
I need to test this class in a program that creates a date object with default parameters and another with program defined ones (no need to ask the user) and uses the class functions to print those dates using the three different printing functions.

This is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;

class Date
{
private:
	int month;
	int day;
	int year;
public:
	void getInfo( int month, int day, int year);
	void printOne(void);
	void printTwo(void);
	void printThree(void);
	Date();
};
void Date::getInfo(int m, int d, int y)
{
	month=m;
	day=d;
	year=y;
}
void Date::printOne()
{
	switch(month)
	{
	case 1:
		cout<<"January"<<day<<","<<year;
		break;
	case 2:
		cout<<<"February"<<day<<","<<year;
		break;
	case 3:
		cout<<"March"<<day<<","<<year;
		break;
	case 4:
		cout<<"April"<<day<<","<<year;
		break;
	case 5:
		cout<<"May"<<day<<","<<year;
		break;
	case 6:
		cout<<"June"<<day<<","<<year;
		break;
	case 7:
		cout<<"July"<<day<<","<<year;
		break;
	case 8:
		cout<<"August"<<day<<","<<year;
		break;
	case 9:
		cout<<"September"<<day<<","<<year;
		break;
	case 10:
		cout<<"October"<<day<<","<<year;
		break;
	case 11:
		cout<<"November"<<day<<","<<year;
		break;
	case 12:
		cout<<"December"<<day<<","<<year;
		break;
	}
}
Date::Date()
{month=1;day=1;year=2001;}

void Date::printTwo()
{
	cout<<month<<"/"<<day<<"/"<<year<<endl;
}


I really need to know if I am on the right track with using classes. For the printout function I feel like i need to use a switch in order to convert the numbers into strings i.e. 3=march or 12=december. ANY advice is APRECIATED!!
Last edited on
Maybe something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

const int BUFFER_SIZE = 256;

const int daysinMonth[13] = 
	{
		-1, 31, 28, 31, 30, 31, 30,
		31, 31, 30, 31, 30, 31
	};

const string nameOfMonth[13] = 
	{
	"", "January", "February", "March", 
	"April", "May", "June", "July", 
	"August", "September", "October", 
	"November", "Decemeber"
	};

class Date
{
private:
	int month;
	int day;
	int year;
	void CheckInput(int m, int d, int y);
	bool isLeapYear(int y);


public:
	Date(int m = 1, int d = 1, int y = 2001); // constructor
	void getInfo(); // accessor
	void printOne(void);
	void printTwo(void);
	void printThree(void);
};

Date::Date(int m, int d, int y)
{
	CheckInput(m, d, y);
	month = m;
	day = d;
	year = y;					
}

void Date::CheckInput(int m, int d, int y)
{
	ostringstream daysinMonthErrorMsg;

	if((m < 1) || (m > 12))
		throw range_error("A month must be between 1 and 12");

		isLeapYear(y);
		daysinMonthErrorMsg.str("");
		daysinMonthErrorMsg << "A day must between 1 and ";
		daysinMonthErrorMsg << daysinMonth[m] 
			<< " for the month of ";
		daysinMonthErrorMsg	<< nameOfMonth[m] 
			<< " in " << y << endl;

	if(m == 2 && isLeapYear(y))
	{
		if((d < 1) || (d > 29))
			throw range_error("A day must between 1 and 29 for the month of February in a leap year");
	}
	else if((d < 1) || (d > daysinMonth[m]))
	{
			throw range_error(daysinMonthErrorMsg.str());
	}

	if(y < 1)
		throw range_error("A year cannot be negative");

}

bool Date::isLeapYear(int y)
{
	if(!(y % 4))
	{
		return true;
	}
	else
		return false;
}

void Date::getInfo()
{
	cout << "Class Info: \n";
	cout << month << endl;
	cout << day << endl;
	cout << year << endl;
}
void Date::printOne()
{
	cout << month << "/" << day << "/" << year<< endl;
}

void Date::printTwo()
{
	cout << nameOfMonth[month] << " " << day << ", " << year << endl;
}

void Date::printThree()
{
	cout << day << " " << nameOfMonth[month] << " " << year << endl;
}

int main()
{
	try
	{

	cout << "Leap year test: \n";
	Date datetest(2, 29, 2016); // leap year, 29 days in February
	datetest.printOne();
	datetest.printTwo();
	datetest.printThree();

	cout << '\n';
	cout << "3 parameters test: \n";
	Date datetest2(8, 31, 2005); // testing regular
	datetest2.printOne();
	datetest2.printTwo();
	datetest2.printThree();

	cout << '\n';
	cout << "No parameters test: \n";
	Date datetest3; // testing no parameter
	datetest3.printOne();
	datetest3.printTwo();
	datetest3.printThree();

	cout << '\n';
	cout << "getinfo() test - \n";
	datetest.getInfo();

	//error tests, can only do one at a time

	/*cout << '\n';

	// tests out of range month
	Date datetest4(0, 31, 2005); 
	datetest4.printOne();
	datetest4.printTwo();
	datetest4.printThree();*/

	/*cout << '\n';

	// tests out of range day, non-leap year
	Date datetest5(2, 29, 2014); 
	datetest5.printOne();
	datetest5.printTwo();
	datetest5.printThree();*/

	/*cout << '\n';

	// tests out of range year
	Date datetest6(2, 3, -1); 
	datetest6.printOne();
	datetest6.printTwo();
	datetest6.printThree();*/

	}
	catch(exception e)
	{
		cerr << e.what() << endl;
		cin.ignore(BUFFER_SIZE, '\n');
		return EXIT_FAILURE;
	}
	cin.ignore(BUFFER_SIZE, '\n');
	return EXIT_SUCCESS;
}


This accounts for leap years. It throws range errors for bad input. I wasn't sure how you wanted to handle the errors. Also for getInfo(), I couldn't tell if you were going to use it as an accessor or a mutator. I made it an accessor that displays raw class data. setInfo() or resetInfo() is what I would call a mutator if you are seeking a way to change the class data after it is declared.
Topic archived. No new replies allowed.