An Event Class with Time and Date objects

This is the instructions of our project:

Create a new project which consists of three classes; an Event class, the Time class and a Date class.

The Event class should have data members that are objects of the Time and Date class.

Extend the code for the Event and Time classes given in the lecture notes to include all necessary function and constructor implementation code. Also design a Date class which has three data members to represent the year, month and the day.

Date class member functions should be able to :

* set the date
* return the date
* print the date
* add a default constructor and a constructor with parameters.

Construct the main method so that it can test the member functions of each of the classes by creating an Event object and then performing the necessary operations to test all possible behavior

-------------------------------------------------------------------

And this is the code:

#include <string>
#include <iostream>

using namespace std;

class Time
{

public:
void setTime(int hour, int minute);
void getTime(int&hr, int&min);
void printTime() const;
Time(int hour, int minute);
Time();
private:
int hour;
int minute;
};

class Date
{
public:
Date(int month, int day, int year);
Date();
void getDate(int&mm, int&dd, int&yy);
void setDate(int month = 1, int day = 1, int year = 1900);
void printDate();
private:
int month;
int day;
int year;
};

class Event
{
public:
void setEventData(string eventName; int hour, int minute, int month, int day, int year);
void printEventData();
Event (string eventName= "Event", int hour = 0, int minute = 0,int month = 1, int day = 1, int year = 1900);

private:

Time eventTime;
Date eventDate;
string eventName;

};

Time::Time(){hour= minute = 0;}
Time::Time (int hr, int min) {hour = hr; minute = min;}
void Time::getTime( int &hr, int &min) { hr = hour; min = minute;}
void Time::setTime( int hr, int min) { hour = hr; minute = min;}
void Time::printTime()
{

int hr, int min;
getTime (hr, min);
if (hr < 20) cout << "0";
cout << hr << ":";
if (min < 20) cout << "0";
}
Date::Date() {month = day = 1; year = 1900;}
Date::Date()(int month, int day, int year);
void Date::getDate(int &mm, int &dd, int &yy) { mm = month; dd = day; yy = year; }
void Date::setDate(int mm, int dd, int yy) { month = mm; day = dd; year = yy; }
void Date::printDate()
{
int mm, dd, yy;
getDate(mm, dd, yy);
if (mm < 20) cout << "0";
cout << mm << "/";
if (dd < 20) cout << "0";
cout << dd << "/";
if (yy < 100) cout << "20";
cout << year;

}

Event::Event(string name, int hour, int minute, int month, int day, int year)
: eventTime(hour,minute), eventDate(month,day,year)
{
eventName = name;
}

void Event::setEventData(string name, int hr, int min, int mon, int day, int yr)
{
eventName = name;
eventTime.setTime(hr, min);
eventDate.setDate(mon, day, yr);
}

void Event::printEventData()
{
cout << eventName << " occurs ";
eventDate.printDate();
cout << " at ";
eventTime.printTime();
}

int main()

{

Event event("New Year's Day", 0, 1, 1, 1, 2008);
event.printEventData();
cout << endl;
event.setEventData("Mother's Day");
event.printEventData();
cout << endl;
return 0;
}

-------------------------------------------------------------------

Here are the errors:

error C2143: syntax error : missing ')' before ';'
error C2062: type 'int' unexpected
error C2511: 'void Time::printTime(void)' : overloaded member function not found in 'Time'
see declaration of 'Time'
error C2091: function returns function
error C2761: '{ctor}' : member function redeclaration not allowed
error C2511: 'void Event::setEventData(std::string,int,int,int,int,int)' : overloaded member function not found in 'Event'

-------------------------------------------------------------------

I am a C++ beginner and am still learning, so please be a little kind while commenting. I have been working on this for quite some time, and am at wits end. Thank you for the help.
I fixed it so it compiles but you still have a few problems in your functions. See if you can figure out why it's not displaying properly. I'd fix it all but that would be you not learning, i left a few comments where I changed a few small things.
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
#include <string>
#include <iostream>

using namespace std;

class Time
{

public:
	void setTime(int hour, int minute);
	void getTime(int&hr, int&min);
	// removed const
	void printTime();
	Time(int hour, int minute);
	Time();
private:
	int hour;
	int minute;
};

class Date
{
public: 
	Date(int month, int day, int year);
	Date();
	void getDate(int&mm, int&dd, int&yy);
	void setDate(int month = 1, int day = 1, int year = 1900);
	void printDate();
private:
	int month;
	int day;
	int year;
};

class Event
{
public:
	// removed the ; after eventName
	void setEventData(string eventName, int hour, int minute, int month, int day, int year);
	void printEventData();
	Event (string eventName= "Event", int hour = 0, int minute = 0,int month = 1, int day = 1, int year = 1900); 

private:

	Time eventTime;
	Date eventDate;
	string eventName;

};

Time::Time(){hour= minute = 0;} 
Time::Time (int hr, int min) {hour = hr; minute = min;}
void Time::getTime( int &hr, int &min) { hr = hour; min = minute;}
void Time::setTime( int hr, int min) { hour = hr; minute = min;}
void Time::printTime()
{
	// removed second 'int'
	int hr, min;
	getTime (hr, min);
	if (hr < 20) cout << "0";
	cout << hr << ":";
	if (min < 20) cout << "0";
}
Date::Date() {month = day = 1; year = 1900;}
// fixed brackets
Date::Date(int month, int day, int year)
{
}
void Date::getDate(int &mm, int &dd, int &yy) { mm = month; dd = day; yy = year; }
void Date::setDate(int mm, int dd, int yy) { month = mm; day = dd; year = yy; }
void Date::printDate() 
{ 
	int mm, dd, yy;
	getDate(mm, dd, yy);
	if (mm < 20) cout << "0";
	cout << mm << "/";
	if (dd < 20) cout << "0";
	cout << dd << "/";
	if (yy < 100) cout << "20";
	cout << year;

}

Event::Event(string name, int hour, int minute, int month, int day, int year)
: eventTime(hour,minute), eventDate(month,day,year)
{
	eventName = name;
}

void Event::setEventData(string name, int hr, int min, int mon, int day, int yr)
{
	eventName = name;
	eventTime.setTime(hr, min);
	eventDate.setDate(mon, day, yr);
}

void Event::printEventData()
{
	cout << eventName << " occurs ";
	eventDate.printDate();
	cout << " at ";
	eventTime.printTime();
}

int main()

{

	Event event("New Year's Day", 0, 1, 1, 1, 2008);
	event.printEventData();
	cout << endl;
	// fill data
	event.setEventData("Mother's Day", 1, 1, 1, 1, 2000);
	event.printEventData();
	cout << endl;
	return 0;
} 
Thanks for your help. I see the problem that you are talking about. Hopefully I can fix it on my own. This is truly going to be an arduous, but rewarding task when finished.
Last edited on
Well I can tell you that this is where your problem is. The ctor eventDate has not been populated inside the function.
1
2
3
4
5
Event::Event(string name, int hour, int minute, int month, int day, int year)
: eventTime(hour,minute), eventDate(month,day,year)
{
	eventName = name;
}

You can use it the same way you use:
1
2
3
4
5
6
void Event::setEventData(string name, int hr, int min, int mon, int day, int yr)
{
	eventName = name;
	eventTime.setTime(hr, min);
	eventDate.setDate(mon, day, yr);
}


But you'll want to change Date::Date to this so it's not left empty.
1
2
3
4
5
6
Date::Date(int month, int day, int year)
{
	this->month = month;
	this->day = day;
	this->year = year;
}


You need to make it so the date can be initialized and then the way you were doing it would work.
Last edited on
Topic archived. No new replies allowed.