Inheritance

I have my code almost all done, and I get no errors, but I can't get my Timezone to print when I call it. Can anyone point me in the right direction, what am I missing?

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

using namespace std;

class Time
{
public:
	Time();
	Time(int hour, int minute);
	void setHour(int);
	void setMinute(int);
	int getHour();
	int getMinute();
	void incrHour(int);
	void incrMinute(int);
	void printTime();

private:
	int hr, min;
};

class extTime: public Time
{
public:
	extTime();
	extTime(int hr, int min, string tz);
	string getTimezone();
	void setTimezone(string);
	void printTime();

private:
	string tz;
};

Time::Time()
{
	hr = min = 0;
}

Time::Time(int hour, int minute)
{
	hr = hour; min = minute;
}

void Time::setHour(int hour)
{
	hr = hour;
}

void Time::setMinute(int minute)
{
	min = minute;
}

int Time::getHour()
{
	return hr;
}

int Time::getMinute()
{
	return min;
}

void Time::incrHour(int addHr)
{
	hr += addHr;
	if(hr == 24) hr = 0;
	if(hr > 24) hr = hr %24;
	if(hr < 0) hr = hr * -1;
}

void Time::incrMinute(int addMin)
{
	min += addMin;
	if(min > 59){ hr++;	min = min %60;}
	if(min < 0)	min = min * -1;
}

void Time::printTime()
{
	if(min < 10) cout << " time is " << hr << ":" << "0" << min << endl;
	else cout << " time is " << hr << ":" << min << endl;
}

extTime::extTime()
{
	tz = " ";
}

extTime::extTime(int hr, int min, string tz)
:Time(hr, min)
{
	tz = tz;
}

string extTime::getTimezone()
{
	return tz;
}

void extTime::setTimezone(string tz)
{
	tz = tz;
}

void extTime::printTime()
{
	if(getMinute() < 10) 
		cout << "\n" << getHour() << ":" << "0" << getMinute() << " " << getTimezone() << endl;
	else 
		cout << "\n" << getHour() << ":" << getMinute() << " " << getTimezone() << endl << endl;
}

int main()
{
	Time day1(5, 10);  // create a Time object
	cout << "The initial"; day1.printTime();	// prints a Time object

	day1.setHour(5); day1.setMinute(30);  // set the hour and minute of Time object
	cout << "After being set the"; day1.getHour();	day1.getMinute();	// return the values of hr and min
	day1.printTime();	// prints a Time object

	day1.incrHour(27); day1.incrMinute(50);  // makes the values of hr & min go over a normal amount allowed	
	cout << "Incremented by 27 hours and 50 minutes the"; day1.printTime();	// prints a Time object after change

	extTime zone;  // create extTime object
	zone.setHour(2); zone.setMinute(12); zone.setTimezone("SGT");
	zone.printTime();



	return 0;
}


I can make it work, sort of, if I type it in in extTime's default constructor, but I want to be able to call it from the code in Main.
Last edited on
Prints 2:12 for me. Is it meant to print that?
It should also print the timezone string after the time.
closed account (1vRz3TCk)
1
2
3
4
void extTime::setTimezone(string tz)
{
	tz = tz;
}

Here you have the local variable tz assigned to the local variable tz. You would need to change it to this->tz = tz;. This goes for anywhere where you have a local variable name the same as a class member name.

NB. function parameters are local variables.
Last edited on
Thank you very much, it works. What is the NB for? I am curious if there is another way, because the instructor in our live lecture didn't use this-> when making his example. I followed the example of the instructor, and I watched the lecture many times. He didn't continue his example til there was an output screen though.

I can put some more comments in now and divide the program up into different headers and .cpp files.
Last edited on
closed account (1vRz3TCk)
What is the NB for?

I don't quite sure what you are asking

First off NB is shorthand for 'nota bene' and is a Latin phrase meaning "note well", "pay attention" or "take notice".

The actual note, function parameters are local variables, was made because I decided that you may not have been told that yet.

I am curious if there is another way

The only other way that I know of it not to have the names the same i.e.
1
2
3
4
void extTime::setTimezone(string Tz)
{
	tz = Tz;
}
Given your other accessors, if you'd been consistent with your naming you wouldn't have hit the problem!

hr = hour
min = minute
tz = timezone ???

I rarely see this-> in code at work; it is generally seen as poor style. You only use it when you really have to.

Some people, including me, mark their member variabled with a prefix or suffix. If you do this the problem goes away. And I find it helps me to follow the code.

Where I've worked (all companies) use an m_ prefix to mark all member variable. This comes from of MFC and ATL, which are libraries I've frequently encountered. Other styles are underscore prefixes and suffixes.

Some people reverse the strategy and use (e.g.) an underscore prefix for their parameter names. Less commonly I have seen a p_ prefix used.
closed account (1vRz3TCk)
A prefix underscore (i.e. _name) is reserved by the standard and should be avoided.
Strictly speaking, the convention on underscore prefixes applies to identifiers in the global namespace. And it's _ followed by a capital letter or another _ which are reserved. So _ followed by a lower case letter is ok.

That said, all the coding conventions I've worked under avoid all use of _ prefixes for consistency. But I do bump into examples using _ followed by lower case letters, so other people obviously have a different opinion/habit.

Andy

P.S. Even though the rules is for globals, macros could potentially cause carnage to a poorly named member variables.
Last edited on
closed account (1vRz3TCk)
for completeness sake:
17.4.3.1.2 Global names [lib.global.names]
1 Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore (__) or begins with an underscore followed by an upper-case letter (2.11) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.


Thank you for all of the information- it really helps.
Topic archived. No new replies allowed.