assistance with class code

Hello, been having issues with this program that uses classes. Any help would be appreciated, but please make it as simple as possible.

This chapter defines the class clockType to implement time in a program. Add functions to this class so that a program that uses this class can set only the hours, minutes, or seconds and retrieve only the hours, minutes, or seconds. Make the functions that retrieve hours, minutes, and seconds as inline. Also, write a program to test your class.

The header file for the class clockType, as it is defined in this chapter, has been provided for you.

Your output should look like this :

myClock: 05:04:30
yourClock: 00:00:00
After setting, yourClock: 05:45:16
Enter the hours, minutes, and seconds: 1 1 1

myClock: 01:01:01
After incrementing myClock by one second, myClock: 01:01:02
hours = 1, minutes = 1, seconds = 2

clockTypeImp.cpp must not be empty. You are not permitted to place code that implements the clockType class in main.cpp. main must only create objects of type clockType and make calls to the functions of the clockType class.


clocktype.h class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class clockType

{

public:
 clockType();
 clockType(int h, int m, int s);
 clockType(int h, int m);
 void setTime(int h, int m, int s);
 void getTime(int&, int&, int&) const;
 void printTime() const;
 void incrementSeconds();
 void incrementMinutes();
 void incrementHours();
 bool equalTime(const clockType&) const;

private:

 int hr;
 int min;
 int sec;

};


clockTypeImp.cpp:
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
#include "clockType.h"
#include <iostream>
#include <fstream>

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

clockType::clockType(int h, int m){
    hr = h;
    min = m;
    sec = 0;
}

clockType::clockType(int h, int m, int s){
    setTime(h, m, s);
}

void clockType::setTime(int h, int m, int s){
    if(0 <= h && h < 24){
        hr = h;
    }
    else{
        hr = 0;
    }
    if(0 <= m && m < 60){
        min = m;
    }
    else{
        min = 0;
    }
    if(0 <= s && s < 60){
        sec = s;
    }
    else{
        sec = 0;
    }
}

ostream& operator<<(ostream& os, const clockType& out)
{
    os << "Hour is " << out.hr << "Minute is " << out.min << "Seconds is " << out.sec;
    return os;
}

clockType operator+(const clockType& one, const clockType& two)
{
    clockType time;
    time.hr = one.hr + two.hr;
    time.min = one.min + two.min;
    time.sec = one.sec + two.sec;
    return time;
}


main:
1
2
3
4
5
6
7
8
9
int main()
{
    clockType c1(5, 10, 15), c2(3, 20); 
    cout << "c1 is " << c1;  
    cout << "c2 is " << c2;
    cout << "c1+c2 is " <<  c1+c2;
    c2 = c1+c1;
    cout << "c1+c1 is " << c2;
}
Last edited on
Hello, been having issues with this program that uses classes.

What "issues"? What exactly is the problem? We're not mind-readers!
Last edited on
Hello, sorry for not being specific enough. Ill put the errors that it shows in. I don't really know how to use clockTypeImp.cpp. The assignment calls for us to use it, to use main.cpp (only for objects), and to use clockType.h.

here is my updated code:

for clockType.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class clockType

{

public:
 clockType(int h = 0, int m = 0, int s = 0);
 void setTime(int h, int m, int s);
 void getTime(int& h, int& m, int& s) const;
 void printTime() const;
 void incrementSeconds();
 void incrementMinutes();
 void incrementHours();
 bool equalTime(const clockType&) const;

private:

 int hr;
 int min;
 int sec;

};


for clockTypeImp.cpp:
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
#include "clockType.h"
#include <iostream>
#include <fstream>

clockType::clockType(int h, int m, int s){
    hr = h;
    min = m;
    sec = s;
}

void clockType::setTime(int h, int m, int s){
    hr = h;
    min = m;
    sec = s;
}

void clockType::printTime(){
    cout << setw(2) << setfill('0') << hr << ":" << setw(2) << setfill('0') << min << ":" << setw(2) << setfill('0') << sec << "\n"
}

bool clockType::equalTime(clockType second){
    if(hr == second.hr && min == second.min && sec = second.sec){
        return true;
    }
    else{
        return false;
    }
}


for main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "clockType.h"
#include "clockTypeImp.cpp"
using namespace std;

int main() {
    // Write your main here
    clockType time1(5, 10, 15);
    time1.print();
    clockType time2;
    time2.print();
    time2.setTime(6, 39, 33);
    time2.print();
    
}


here are the errors I'm getting:

clockTypeImp.cpp:17:6: error: prototype for ‘void clockType::printTime()’ does not match any in class ‘clockType’
void clockType::printTime(){
^~~~~~~~~
clockType.h:9:7: error: candidate is: void clockType::printTime() const
void printTime() const;
^~~~~~~~~
clockTypeImp.cpp:21:6: error: prototype for ‘bool clockType::equalTime(clockType)’ does not match any in class ‘clockType’
bool clockType::equalTime(clockType second){
^~~~~~~~~
clockType.h:13:7: error: candidate is: bool clockType::equalTime(const clockType&) const
bool equalTime(const clockType&) const;
^~~~~~~~~
In file included from clockTypeImp.cpp:1:0,
from main.cpp:3:
clockType.h:1:7: error: redefinition of ‘class clockType’
class clockType
^~~~~~~~~
In file included from main.cpp:2:0:
clockType.h:1:7: note: previous definition of ‘class clockType’
class clockType
^~~~~~~~~
In file included from main.cpp:3:0:
clockTypeImp.cpp:17:6: error: prototype for ‘void clockType::printTime()’ does not match any in class ‘clockType’
void clockType::printTime(){
^~~~~~~~~
In file included from main.cpp:2:0:
clockType.h:9:7: error: candidate is: void clockType::printTime() const
void printTime() const;
^~~~~~~~~
In file included from main.cpp:3:0:
clockTypeImp.cpp:21:6: error: prototype for ‘bool clockType::equalTime(clockType)’ does not match any in class ‘clockType’
bool clockType::equalTime(clockType second){
^~~~~~~~~
In file included from main.cpp:2:0:
clockType.h:13:7: error: candidate is: bool clockType::equalTime(const clockType&) const
bool equalTime(const clockType&) const;
^~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:9:11: error: ‘class clockType’ has no member named ‘print’
time1.print();
^~~~~
main.cpp:11:11: error: ‘class clockType’ has no member named ‘print’
time2.print();
^~~~~
main.cpp:13:11: error: ‘class clockType’ has no member named ‘print’
time2.print();

sory for spelling mistakes, my english is not that good.
Here's a starter.

In clockType.h, printTime() is declared as const. But in clockTypeImp.cpp it doesn't have const. The args and qualifiers must match between the declaration and the implemenation.

Also similar for other functions...
I highly recommend you split the object and the interface. Here the interface is cout, but print-time should return maybe a formatted string that you can then print with cout or send to a file or put in a gui text box etc. If you glue it to cout, the class won't be useful outside of printing to the console. Its homework and not a big deal HERE, but this concept is critical outside of the classroom and often forgotten by the professors.

@jphillips356,

look real close at line 22 in your latest class implementation file. It should be
if (hr == second.hr && min == second.min && sec == second.sec)

Your header file should use header guards.
https://www.learncpp.com/cpp-tutorial/header-guards/

You are not calling the class printTime in main.

Here's your code with fixes:
clockType.h
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
// using header guards is a good idea
// https://www.learncpp.com/cpp-tutorial/header-guards/

#ifndef CLOCK_HPP
#define CLOCK_HPP

class clockType
{
public:
   clockType(int h = 0, int m = 0, int s = 0);

public:
   void setTime(int h, int m, int s);
//   void getTime(int& h, int& m, int& s) const;
//   void incrementSeconds();
//   void incrementMinutes();
//   void incrementHours();
   bool equalTime(const clockType&) const;

public:
   void printTime() const;

private:
   int hr;
   int min;
   int sec;
};

#endif 

clockTypeImp.cpp
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
#include <iostream>
#include <iomanip>  // std::setw, std::setfill

#include "clockType.h"

clockType::clockType(int h, int m, int s)
{
   hr  = h;
   min = m;
   sec = s;
}

void clockType::setTime(int h, int m, int s)
{
   hr = h;
   min = m;
   sec = s;
}

void clockType::printTime() const
{
   std::cout << std::setw(2) << std::setfill('0') << hr << ':'
             << std::setw(2) << std::setfill('0') << min << ':'
             << std::setw(2) << std::setfill('0') << sec << '\n';
}

bool clockType::equalTime(const clockType& second) const
{
   if (hr == second.hr && min == second.min && sec == second.sec)
   {
      return true;
   }
   else
   {
      return false;
   }
}

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

#include "clockType.h"

// never #include your implementation file
// the compiler will link if your make command is proper

int main()
{
   clockType time1(5, 10, 15);
   time1.printTime();

   clockType time2;
   time2.printTime();

   time2.setTime(6, 39, 33);
   time2.printTime();
}
05:10:15
00:00:00
06:39:33
As a refactor and as one file, consider:

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
#include <iostream>
#include <fstream>
#include <iomanip>

class clockType {
public:
	clockType();
	clockType(int h, int m, int s = 0);
	void setTime(int h, int m, int s);
	void getTime(int&, int&, int&) const;
	void printTime() const;
	void incrementSeconds(int s = 1);
	void incrementMinutes( int m = 1);
	void incrementHours(int h = 1 );
	bool equalTime(const clockType&) const;
	clockType operator+(const clockType&) const;

private:
	int hr {};
	int min {};
	int sec {};
};

clockType::clockType() = default;

clockType::clockType(int h, int m, int s) { setTime(h, m, s); }

void clockType::setTime(int h, int m, int s) {
	hr = (0 <= h && h < 24) ? h : 0;
	min = (0 <= m && m < 60) ? m : 0;
	sec = (0 <= s && s < 60) ? s : 0;
}

void clockType::getTime(int& h, int& m, int& s) const {
	h = hr;
	m = min;
	s = sec;
}

void clockType::printTime() const {
	std::cout << std::setw(2) << std::setfill('0') << hr << ":" << std::setw(2) << std::setfill('0') << min << ":" <<
		std::setw(2) << std::setfill('0') << sec << "\n";
}

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

void clockType::incrementSeconds(int s) {
	if ((sec += s) > 60)
		min += sec / 60, sec %= 60;

	if (min > 60)
		hr += min / 60, min %= 60;
}

void clockType::incrementMinutes(int m) {
	if ((min += m) > 60)
		hr += min / 60, min %= 60;
}

void clockType::incrementHours(int h) {
	hr += h;
}

std::ostream& operator<<(std::ostream& os, const clockType& out) {
	int h, m, s;

	out.getTime(h, m, s);
	return os << "Hour is " << h << ", Minute is " << m << ", Seconds is " << s;
}

clockType clockType::operator+(const clockType& one) const {
	clockType time (*this);

	int h, m, s;

	one.getTime(h, m, s);
	time.incrementSeconds(s);
	time.incrementMinutes(m);
	time.incrementHours(h);

	return time;
}

int main() {
	clockType c1(5, 10, 15), c2(3, 20);

	std::cout << "c1 is " << c1 << '\n';
	std::cout << "c2 is " << c2 << '\n';
	std::cout << "c1+c2 is " << c1 + c2 << '\n';

	c2 = c1 + c1;
	std::cout << "c1+c1 is " << c2 << '\n';
}



c1 is Hour is 5, Minute is 10, Seconds is 15
c2 is Hour is 3, Minute is 20, Seconds is 0
c1+c2 is Hour is 8, Minute is 30, Seconds is 15
c1+c1 is Hour is 10, Minute is 20, Seconds is 30

The logic needs to be fixed, times like 17:09:19 and 19:11:01 will produce
c1 is Hour is 17, Minute is 9, Seconds is 19
c2 is Hour is 19, Minute is 11, Seconds is 1
c1+c2 is Hour is 36, Minute is 20, Seconds is 20
c1+c1 is Hour is 34, Minute is 18, Seconds is 38
@thmm, why is a time with the hour greater than 24 considered bad logic? There is no "day" data member in this example code with accompanying program logic for carrying over an "excess" hour value.

Would a time of 24:09:18 be considered wrong? For an elapsed timer it wouldn't be.
Do you know how i could implement getHours(), getMinutes(), getSeconds(), setHours(), setMinutes(), and setSeconds()?
In your class declaration:
int getHours() const;

In your class implementation file:
int getHours() const { return hr; }

OR....

it could be contained in the declaration:
int getHours() const { return hr; }

In your class declaration:
void setHours(int);

class implementation:
void setHours(int hour) { hr = hour; }

OR...

class declaration:
void setHours(int hour) { hr = hour; }

Adding the minutes and seconds getters/setters follow the same pattern.
@thmm, why is a time with the hour greater than 24 considered bad logic?

Because it's not a valid time. clockType is probably supposed to represent a time point, not a duration. Imagine your clock would show you a time of 24:09:18.
Wouldn't that be strange?

std::chrono doesn't let you add 2 time points, only a time point and a duration.
That is not bad logic, a clock is just ONE predefined use for a predefined purpose.

An elapsed timer can show hours greater than 24 and still be valid.

An example: a spaceflight of short/medium duration can have events recorded in hours, minutes and seconds. 34 hours is not unheard of being used. The elapsed time of the flight is completely divorced from Earth time, the timer starts when the craft is launched.

https://www.nasa.gov/mission_pages/mercury/missions/faith7.html

An orbiting spacecraft not in geosynchronous orbit is never in the same Earth bound time zone all the time.

Elapsed days become a factor during long duration space flight of several days duration.

This class isn't std::chrono, it's an classroom exercise. Expecting it to be as comprehensive and focused as the C++ stdlib is foolish.
An elapsed timer can show hours greater than 24 and still be valid.

Certainly but a timer is not a time.

A time point is a certain time like 10:11:11. I hope you don't propose to meet someone at 25:12:19. An invalid time point is totally useless.

A duration is the amount of time like 45 hours, 11 min... like a jouney
Hello,

sorry for not responding at all, was in the hospital for 3 days. Here is my updated code:

clockType.h:
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
class clockType

{

public:
 clockType();
 clockType(int h = 0, int m = 0, int s = 0);
 void setTime(int h, int m, int s);
 void getTime(int& , int& , int& ) const;
 void printTime() const;
 void incrementSeconds(int s = 1);
 void incrementMinutes(int m = 1);
 void incrementHours(int h = 1);
 bool equalTime(const clockType&) const;
 int getHours() const;
 int getMinutes() const;
 int getSeconds() const;
 int setHours();
 int setMinutes();
 int setSeconds();

private:

 int hr;
 int min;
 int sec;

};



clockTypeImp.cpp:
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
#include "clockType.h"
#include <iostream>
#include <iomanip>
using namespace std;

clockType::clockType(int h, int m, int s){
    setTime(h, m, s);
}

void clockType::setTime(int h, int m, int s){
    hr = h;
    min = m;
    sec = s;
}

void clockType::getTime(int& h, int& m, int& s) const{
    h = hr;
    m = min;
    s = sec;
}

void clockType::printTime() const{
    cout << setw(2) << setfill('0') << hr << ":" 
         << setw(2) << setfill('0') << min << ":"  
         << setw(2) << setfill('0') << sec << "\n";
}

int clockType::getHours() const{
    return hr;
}

int clockType::getMinutes() const{
    return min;
}

int clockType::getSeconds() const{
    return sec;
}

bool clockType::equalTime(const clockType& second) {
    if(hr == second.hr && min == second.min && sec == second.sec){
        return true;
    }
    else{
        return false;
    }
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "clockType.h"

using namespace std;

int main() {
    // Write your main here
    clockType time1(5, 10, 15);
    cout << "myClock: ";
    time1.printTime();
    clockType time2();
    cout << "yourClock: ";
    time2.printTime();
    time2.setTime(6, 39, 33);
    cout << "After setting, yourClock: ";
    time2.printTime();
}
 
      
   
sorry for not responding at all, was in the hospital for 3 days. Here is my updated code:


Hope you're now OK.

Is there a question somewhere?
Topic archived. No new replies allowed.