Not getting the correct output when testing my program

I was asked to include a derived class on my clockType time.
When I test the program after adding the time zone in the derived class I get wrong results

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
  #include <iostream>
#include <string>
using namespace std;

class clockType
{
public:
  void setTime(int hours, int minutes, int seconds);
  void getTime(int& hours, int& minutes, int& seconds) const;
  void printTime() const;
  void incrementSeconds();
  void incrementMinutes();
  void incrementHours();
  bool equalTime(const clockType& otherClock) const;
  clockType(int hours, int minutes, int seconds);
  clockType();

private:
  int hr, min, sec;
};

void clockType::setTime(int hours, int minutes, int seconds)
{
  if (0 <= hours && hours < 24)
    hr = hours;
  else
    hr = 0;
  if (0 <= minutes && minutes < 60)
    min = minutes;
  else
    min = 0;
  if (0 <= seconds && seconds < 60)
    sec = seconds;
  else
    sec = 0;
}

void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
  hours = hr;
  minutes = min;
  seconds = sec;
}

void clockType::incrementHours()
{
  hr++;
  if (hr > 23)
    hr = 0;
}

void clockType::incrementMinutes()
{
  min++;
  if (min > 59)
   {
     min = 0;
     incrementHours();
   }
}

void clockType::incrementSeconds()
{
 sec++;
 if (sec > 59)
  {
   sec = 0;
   incrementMinutes();
  }
}

void clockType::printTime() const
{
 if (hr < 10)
   cout << "0" << hr << ":";
 if (min < 10)
   cout << "0" << min << ":";
 if (sec < 10)
   cout << "0" << sec;
}

bool clockType::equalTime(const clockType& otherClock) const
{
  return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec);
}

clockType::clockType(int hours, int minutes, int seconds)
{
  if (0 <= hours && hours < 24)
    hr = hours;
  else
    hr = 0;
  if (0 <= minutes && minutes < 60)
    min = minutes;
  else
    min = 0;
  if (0 <= seconds && seconds < 60)
    sec = seconds;
  else
    sec = 0;
}

clockType::clockType()
{
 hr = 0;
 min = 0;
 sec = 0;
}

class extclockType : public clockType
{
public:
 extclockType();
 extclockType(int hours, int minutes, int seconds, string timezone);
 void print() const;
 void setTime(int hours, int minutes, int seconds, string timezone);
  //string getTimeZone();

private:
 string tz;
};

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

extclockType::extclockType(int hours, int minutes, int seconds, string timezone)
:clockType(hours, minutes, seconds)//this is where the error is coming in
{
 tz = timezone;
}

void extclockType::setTime(int hours, int minutes, int seconds, string timezone)
{
 tz = timezone;
}


int main()
{
 int h1, m1, s1, h2, m2, s2;
 string zn1, zn2;

 cin >> h1 >> m1 >> s1 >> zn1;

 cin >> h2 >> m2 >> s2 >> zn2;

 extclockType time1(h1, m1, s1, zn1);

 extclockType time2;

 cout << "Time 1: ";
 time1.printTime();
 cout << endl;

 time2.setTime(h2, m2, s2, zn2);

 cout << "Time 2: ";
 time2.printTime();
 cout << endl;

 time2.incrementSeconds();

 cout << "After incrementing time2 by one second, Time 2: ";
 time2.printTime();
 cout << endl;

 return 0;
}


please help
What are the inputs, and what is the output you're getting/expecting?
The inputs are: hours1, minutes1, seconds1 , USA
hours2, minutes2, seconds2 , London

Here is what I get:

3
4
5
USA
4
5
7
London
Time 1: 03:04:05
Time 2: 00:00:00
After incrementing time2 by one second, Time 2: 00:00:01

Process returned 0 (0x0) execution time : 17.217 s
Press any key to continue.




Your extclockType::setTime only writes to ONE of the member variables.
1
2
3
4
void extclockType::setTime(int hours, int minutes, int seconds, string timezone)
{
 tz = timezone;
}


time1 works, because it's all initialised properly via constructor.
Okay I have corrected that and now I get:
2
3
4
London
5
6
7
USA
Time 1: 02:03:04
Time 2: 05:06:07
After incrementing time2 by one second, Time 2: 05:06:08

Process returned 0 (0x0) execution time : 18.276 s
Press any key to continue.

so far so good!!!!
now getting errors with the inherited extclockType:
C:\Martin\MalikChapter2\clockInheritance\extClock\helpClock.cpp:141:22: error: no match for 'operator<<' in 'std::cout << ((const extclockType*)this)->extclockType::<anonymous>.clockType::printTime()'

here is the code for the added inherited extclockType for printing the time zone

1
2
3
4
5
void extclockType::print() const
 {
   cout << printTime();
   cout << tz;
 }


printTime() itself does the cout'ing. You don't need "cout <<" before it.
Thanks very much LastChance!!! it works like a bomb....
Topic archived. No new replies allowed.