Getting wrong values with class composition

I'm trying to figure out how to use composite classes, but I'm having a lot of trouble getting the values correct. The output gives me a -889888282 number instead of the variable, which would lead me to believe that it is never getting a value and it is staying default. Here is my code.

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
#include <iostream>
#include <string>

using namespace std;

//
class Date
{
public:

	int m;//
	int d;//
	int y;//I didn't want to put these in here, but I get an error in my event call if I don't!)
	Date();
	Date(int m, int d, int y);
	void setDate(int m, int d, int y);
	int getDate();
	void printDate();

private:

	int month;
	int day;
	int year;

};
//
/* Date constructors */
Date::Date()
{
	//default constructor
}
//
Date::Date(int m, int d, int y)
{
	month = m;
	day = d;
	year = y;
}
//
/* Date methods */
void Date::setDate(int m, int d, int y)
{
	month = m;
	day = d;
	year = y;
}
//
int Date::getDate()
{
	return month, day, year;
}
//
void Date::printDate()
{
	setDate(m, d, y);
	getDate();
	cout << " " << month << "/" << day << "/" << year << ". ";
}
//
//
class Time
{
public:

	int hr;//
	int min;//same thing as m d y above, I get an error if I don't put these here.
	Time();
	Time(int hr, int min);
	void setHour(int hr);
	void setMinute(int min);
	int getHour();
	int getMinute();
	void printTime();

private:

	int hour;
	int minute;
};
//
/* Time constructors */
Time::Time()
{
	//default
}
//
Time::Time(int hr, int min)
{
	hour = hr;
	minute = min;
}
//
/* Time methods */
void Time::setHour(int hr)
{
	hour = hr;
}
//
void Time::setMinute(int min)
{
	minute = min;
}
//
int Time::getHour()
{
	return hour;
}
//
int Time::getMinute()
{
	return minute;
}
//
void Time::printTime()
{
	cout << hour << ":" << minute;
}
//
class Event
{

public:

	Event();
	Event(string, int, int, int, int, int);
	void printEvent();

private:

	string eventName;
	int eventMonth;
	int eventDay;
	int eventYear;
	int eventHour;
	int eventMinute;
	Time eventTime;
	Date eventDate;

};
//
/* Event constructors */
Event::Event()
{
	//default
}
//
Event::Event(string evntNm, int eventMonth, int eventDay, int eventYear, int eventHour, int eventMinute) : eventTime(eventTime.hr, eventTime.min), eventDate(eventDate.m, eventDate.d, eventDate.y)
{
	eventName = evntNm;
}
//
/* Event methods */
void Event::printEvent()
{
	cout << endl << "Today is " << eventName << ".  It is "; eventDate.printDate(); cout << " at "; eventTime.printTime(); cout << " AM. " << endl << endl;
}
//
/* Main program */
int main()
{
	Event evnt("Boxing Day",12,26,2011,12,00);
	evnt.printEvent();
	system("pause");
	return 0;
}


The program is pretty self-explanatory I think. Just when I get classes down I run into problems with this :(

Also, if I'm going in the totally wrong direction with what I'm trying to do, please let me know.
Last edited on
shameless bump back to page 1 (I'm sorry =/)
Lets make some modifications and you can compare to you existing code:
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
#include <iostream>
#include <string>

using namespace std;

//
class Date
{
public:

    Date();
    Date(int m, int d, int y);
    void setDate(int m, int d, int y);
    int getDate();
    void printDate();

private:

    int month;
    int day;
    int year;

};
//
/* Date constructors */
Date::Date()
{
    //default constructor
}
//
Date::Date(int m, int d, int y)
:month(m), day(d), year(y)
{

}
//
/* Date methods */
void Date::setDate(int m, int d, int y)
{
    month =m;
    day =d;
    year =y;
}

//*** This function is a problem
int Date::getDate()
{
    return month;//, day, year; //You cannot return 3 things ....
}

//
void Date::printDate()
{
    cout << " " << month << "/" << day << "/" << year << ". ";
}
//
//
class Time
{
public:

    Time();
    Time(int hr, int min);
    void setHour(int hr);
    void setMinute(int min);
    int getHour();
    int getMinute();
    void printTime();

private:

    int hour;
    int minute;
};
//
/* Time constructors */
Time::Time()
{
    //default
}
//
Time::Time(int hr, int min)
:hour(hr), minute(min)
{

}
//
/* Time methods */
void Time::setHour(int hr)
{
    hour = hr;
}
//
void Time::setMinute(int min)
{
    minute = min;
}
//
int Time::getHour()
{
    return hour;
}
//
int Time::getMinute()
{
    return minute;
}
//
void Time::printTime()
{
    cout << hour << ":" << minute;
}
//
class Event
{

public:

    Event();
    Event(string, int, int, int, int, int);
    void printEvent();

private:

    string eventName;
    Time eventTime;
    Date eventDate;

};
//
/* Event constructors */
Event::Event()
{
    //default
}

//
Event::Event(string evntNm, int eventMonth, int eventDay, int eventYear, int eventHour, int eventMinute) 
: eventName(evntNm), eventTime(eventHour, eventMinute), eventDate(eventMonth, eventDay, eventYear)
{
}
//
/* Event methods */
void Event::printEvent()
{
    cout << endl << "Today is " << eventName << ".  It is "; 
    eventDate.printDate(); 
    cout << " at "; 
    eventTime.printTime(); 
    cout << " AM. " << endl << endl;
}
//
/* Main program */
int main()
{
    Event evnt("Boxing Day",12,26,2011,12,00);
    evnt.printEvent();
    system("pause");
    return 0;
}


//**Note the problem with the Date::getDate() function
You cannot return multiple items from a function like this: return month, day, year;
You should have learnt that in Funtions 101 **//
So you need to rethink that function.
ok, thank you. Does this mean that I will have to have 3 set functions for the date and 3 get functions for the date? I did not know that about functions I'm still new to this. I see the other changes you made to the Event constructor, but how will they know where to get the values from without a . call?


Thanks for the help.
you should be able to pass 3 references instead of writing 3 get functions. just a guess though. maybe someone could provide a better example. im not near a compiler right now and dont like to write anything i cant test first.
Thanks, acorn, I will play around with this.
after looking at it a likely better approach is to make month day and year integers part of an structure. then return that structure in a single get statement. this was done fast. it returns a date structure then prints it. that system command wouldnt compile on my system as it stands. i think on non microsoft IDE you might need an additional header file. i had to comment it out for it to compile.

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
#include <iostream>
#include <string>

using namespace std;

struct internaldate
{
        int month;
        int day;
        int year;
};

//
class Date
{
public:

    Date();
    Date(int m, int d, int y);


    void setDate(int m, int d, int y);
    internaldate getDate();
    void printDate();

private:

    internaldate mydate;

};
//
/* Date constructors */
Date::Date()
{
    mydate.day = 1;
    mydate.year = 2011;
    mydate.month = 1;
}
//
Date::Date(int m, int d, int y)
{
    mydate.day = d;
    mydate.year = y;
    mydate.month = m;
}
//
/* Date methods */
void Date::setDate(int m, int d, int y)
{
    mydate.month =m;
    mydate.day =d;
    mydate.year =y;
}

//*** This function is a problem
internaldate Date::getDate()
{
    return mydate;//, day, year; //You cannot return 3 things ....
}

//
void Date::printDate()
{
    cout << " " << mydate.month << "/" << mydate.day << "/" << mydate.year << ". ";
}
//
//
class Time
{
public:

    Time();
    Time(int hr, int min);
    void setHour(int hr);
    void setMinute(int min);
    int getHour();
    int getMinute();
    void printTime();

private:

    int hour;
    int minute;
};
//
/* Time constructors */
Time::Time()
{
    //default
}
//
Time::Time(int hr, int min)
:hour(hr), minute(min)
{

}
//
/* Time methods */
void Time::setHour(int hr)
{
    hour = hr;
}
//
void Time::setMinute(int min)
{
    minute = min;
}
//
int Time::getHour()
{
    return hour;
}
//
int Time::getMinute()
{
    return minute;
}
//
void Time::printTime()
{
    cout << hour << ":" << minute;
}
//
class Event
{

public:

    Event();
    Event(string, int, int, int, int, int);
    void printEvent();

private:

    string eventName;
    Time eventTime;
    Date eventDate;

};
//
/* Event constructors */
Event::Event()
{
    //default
}

//
Event::Event(string evntNm, int eventMonth, int eventDay, int eventYear, int eventHour, int eventMinute)
: eventName(evntNm), eventTime(eventHour, eventMinute), eventDate(eventMonth, eventDay, eventYear)
{
}
//
/* Event methods */
void Event::printEvent()
{
    cout << endl << "Today is " << eventName << ".  It is ";
    eventDate.printDate();
    cout << " at ";
    eventTime.printTime();
    cout << " AM. " << endl << endl;
}
//
/* Main program */
int main()
{
    Event evnt("Boxing Day",12,26,2011,12,00);
    evnt.printEvent();
    Date thisdate(12,26,2011);
    internaldate id = thisdate.getDate();

    cout << id.month << "/" << id.day << "/" << id.year << endl;
    system("pause");
    return 0;
}
Last edited on
Ok, I like the way that looks. I'm afraid to use struct as I have not learned about that yet, though. You don't have to make any changes to the Event constructor when you call that?

How would you have done it passing by reference?
Last edited on
i dont think so. your event constructor calls date with 3 params. those 3 paramaters are loaded into the internal date structure inside the date class. i couldnt find any spot really that actually utilized the getdate function though. if you havent learned structures yet then it may be best not to use them. if you havent done structures then you probrobly havent done references either. so what would be best would be just to use what you learned and create 3 private variables and 3 public get functions for them.
Last edited on
I tried to do it like this but it still does just the same thing.

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>
#include <string>

using namespace std;

//
class Date
{
public:

	int m;
	int d;
	int y;
	Date();
	Date(int m, int d, int y);
	void setMonth(int m);
	void setDay(int d);
	void setYear(int y);
	int getMonth();
	int getDay();
	int getYear();
	void printDate();

private:

	int month;
	int day;
	int year;

};
//
/* Date constructors */
Date::Date()
{
	//default constructor
}
//
Date::Date(int m, int d, int y)
{
	month = m;
	day = d;
	year = y;
}
//
/* Date methods */
void Date::setMonth(int m)
{
	month = m;
}
//
void Date::setDay(int d)
{
	day = d;
}
//
void Date::setYear(int y)
{
	year = y;
}
//
int Date::getMonth()
{
	return month;
}
//
int Date::getDay()
{
	return day;
}
//
int Date::getYear()
{
	return year;
}
void Date::printDate()
{
	setMonth(m);
	setDay(d);
	setYear(y);
	getMonth();
	getDay();
	getYear();
	cout << " " << month << "/" << day << "/" << year << ", ";
}
//
//
class Time
{
public:

	int hr;
	int min;
	Time();
	Time(int hr, int min);
	void setHour(int &hr);
	void setMinute(int &min);
	int getHour();
	int getMinute();
	void printTime();

private:

	int hour;
	int minute;
};
//
/* Time constructors */
Time::Time()
{
	//default
}
//
Time::Time(int hr, int min)
{
	hour = hr;
	minute = min;
}
//
/* Time methods */
void Time::setHour(int &hr)
{
	hour = hr;
}
//
void Time::setMinute(int &min)
{
	minute = min;
}
//
int Time::getHour()
{
	return hour;
}
//
int Time::getMinute()
{
	return minute;
}
//
void Time::printTime()
{
	cout << hour << ":" << minute;
}
//
class Event
{

public:

	Event();
	Event(string, int, int, int, int, int);
	void printEvent();

private:

	string eventName;
	int eventMonth;
	int eventDay;
	int eventYear;
	int eventHour;
	int eventMinute;
	Time eventTime;
	Date eventDate;

};
//
/* Event constructors */
Event::Event()
{
	//default
}
//
Event::Event(string evntNm, int eventMonth, int eventDay, int eventYear, int eventHour, int eventMinute) : eventTime(eventTime.hr, eventTime.min), 
eventDate(eventDate, eventDate, eventDate)/***********I don't know what to do with this line to make it work.**********/
{
	eventName = evntNm;
}
//
/* Event methods */
void Event::printEvent()
{
	cout << endl << "Today is " << eventName << ".  It is "; eventDate.printDate(); cout << " at "; eventTime.printTime(); 
cout << " AM. " << endl << endl;
}/*********I also don't know what to do with this line. ********/
//
/* Main program */
int main()
{
	Event evnt("Boxing Day",12,26,2011,12,00);
	evnt.printEvent();
	system("pause");
	return 0;
}


I've tried so many different things now that I think it's all mixed up. I really appreciate your help.
Last edited on
there was some weird things going on. had to remove a bunch of lines. this works using 3 get functions now. i left a bug for you to fix. it displays 12:10 correctly but not 12:00. so the int doesnt accept 00.

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>
#include <string>

using namespace std;

//
class Date
{
public:

	int m;
	int d;
	int y;
	Date();
	Date(int m, int d, int y);
	void setMonth(int m);
	void setDay(int d);
	void setYear(int y);
	int getMonth();
	int getDay();
	int getYear();
	void printDate();

private:

	int month;
	int day;
	int year;

};
//
/* Date constructors */
Date::Date()
{
	//default constructor
}
//
Date::Date(int m, int d, int y)
{
	month = m;
	day = d;
	year = y;
}
//
/* Date methods */
void Date::setMonth(int m)
{
	month = m;
}
//
void Date::setDay(int d)
{
	day = d;
}
//
void Date::setYear(int y)
{
	year = y;
}
//
int Date::getMonth()
{
	return month;
}
//
int Date::getDay()
{
	return day;
}
//
int Date::getYear()
{
	return year;
}
void Date::printDate()
{
	cout << " " << month << "/" << day << "/" << year << ", ";
}
//
//
class Time
{
public:

	int hr;
	int min;
	Time();
	Time(int hr, int min);
	void setHour(int hr);
	void setMinute(int min);
	int getHour();
	int getMinute();
	void printTime();

private:

	int hour;
	int minute;
};
//
/* Time constructors */
Time::Time()
{
	//default
}
//
Time::Time(int hr, int min)
{
	hour = hr;
	minute = min;
}
//
/* Time methods */
void Time::setHour(int hr)
{
	hour = hr;
}
//
void Time::setMinute(int min)
{
	minute = min;
}
//
int Time::getHour()
{
	return hour;
}
//
int Time::getMinute()
{
	return minute;
}
//
void Time::printTime()
{
	cout << hour << ":" << minute;
}
//
class Event
{

public:

	Event();
	Event(string, int, int, int, int, int);
	void printEvent();

private:

	string eventName;
	int eventMonth;
	int eventDay;
	int eventYear;
	int eventHour;
	int eventMinute;
	Time eventTime;
	Date eventDate;

};
//
/* Event constructors */
Event::Event()
{
	//default
}
//
Event::Event(string evntNm, int eventMonth, int eventDay, int eventYear, int eventHour, int eventMinute)
{
    eventTime.setHour(eventHour);
    eventTime.setMinute(eventMinute);
    eventDate.setDay(eventDay),
    eventDate.setMonth(eventMonth);
    eventDate.setYear(eventYear);
	eventName = evntNm;
}
//
/* Event methods */
void Event::printEvent()
{
	cout << endl << "Today is " << eventName << ".  It is "; eventDate.printDate(); cout << " at "; eventTime.printTime(); cout << " AM. " << endl << endl;
}//I also don't know what to do with this line.
//
/* Main program */
int main()
{
	Event evnt("Boxing Day",12,26,2011,12,10);
	evnt.printEvent();
        system("pause");
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Date::printDate()
{
	setMonth(m); //here m,d,y have garbage
	setDay(d);
	setYear(y);
	getMonth(); //statement has no effect
	getDay();
	getYear();
	cout << " " << month << "/" << day << "/" << year << ", ";
}
//...
class Date
{
public:

	int m;//what are they for?
	int d;
	int y;

If your getter/setter do nothing it's just like have the members public.
Why setter and getter methods are evil http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html?page=1
Last edited on
Ok, I read that. I don't understand it though really, and is Java just like C++ or something?

I was just trying to get the values into the printDate, I don't know how otherwise...

The m, d , y in public was there to stop me from getting errors when i used them in the Event constructor in the first thing I posted.
Yeah, C++ doesn't have properties (things that look like members but are accessors). Whatever, I don't make stuff private unless it would screw stuff up to change them.
Yes, Java is like a slower, more verbose C++. They're very similar.
Last edited on
Thank you acorn I will work with that.
the code can be shrunk down alot for what it does.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using std::cout; using std::endl;
using std::string; using std::cin;

void print(string event, int day, int month, int year, int hour, string minute);

int main(int argc, char **argv)
{
   print("Boxing Day",12,26,2011,12,"00");
   cin.get();
}

void print(string event, int month, int day, int year, int hour, string minute)
{
    cout << "Today is " << event << ".  It is " << day << "/" << month << "/" << year << " ";
    cout << " at "  << hour  << ":" << minute << " AM. " << endl << endl;

}
Wow. Sometimes I kind of question what I'm being taught.


Hey, I'm trying to do:

1
2
3
4
void Time::printTime()
{
	cout << fixed << setprecision(2) << hour << ":" << fixed << setprecision(2) << minute;
}


but it doesn't seem to be working, I thought this was how you forced the 00. I remembered #include <iomanip>
i used a string because i didnt know how to fix it. its the first time ive encountered that problem. ive been focusing more on learning the language then the librarys available. so there is alot of nifty things you can do with formatting i havent played with.
Hm ok. Thanks. I'll mark this solved and deal with this last hiccup, Thank you again.
http://www.cplusplus.com/reference/iostream/manipulators/setfill/

Sometimes I kind of question what I'm being taught.
Object Oriented Programming. The link use java as an example, but is talking about the paradigm.

1
2
3
Event::Event(string evntNm, int eventMonth, int eventDay, int eventYear, int eventHour, int eventMinute) :
    eventTime(eventTime.hr, eventTime.min), 
    eventDate(eventDate.m, eventDate.d, eventDate.y)
You are trying to initializate your objects from the data that they already have (in the garbage state). You are ignoring all the parameters passed.

Topic archived. No new replies allowed.