Class named Time

Hi, so I'm making a class named time that takes a number that the user inputs, for ex 63, and tell the user how many hours, minutes, and seconds that number equals.
Here is what I have so far.
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
  #include <iostream> 
#include <iomanip> 

using std::cout; 
using std::setfill; 
using std::setw; 
using std::cin; 
using std::cout; 
using std::endl; 

class Time 
{
private: 
int hour;  
int minute;  
int second; 
int amount;
             
public:
Time( int = 0, int = 0, int = 0 ); 

const Time Time::operator+(const Time &other) ; 
void setTime( int, int, int );  
void setHour( int );  
void setMinute( int );  
void setSecond( int );  

int getHour() const;  
int getMinute() const;  
int getSecond() const;  

void Time::get( ); 
void Time::show(); 
void change( int amount, int &hours, int &minute, int &seconds);

 
}; 

Time::Time( int hour, int minute, int second ) 
{ 
setTime( hour, minute, second ); 
} 

void Time::setTime( int hour, int minute, int second ) 
{ 
setHour( hour ); 
setMinute( minute ); 
setSecond( second ); 
} 

void Time::get( ) 
{ 
int amount;
cout << "Enter the amount: " << endl;
cin >> amount;
} 

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

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

void Time::setSecond( int s ) 
{ 
second = s; 
} 

int Time::getHour ( ) const 
{ 
return hour; 
} 

int Time::getMinute () const 
{ 
return minute; 
} 

int Time::getSecond () const 
{ 
return second; 
} 

void change(int amount, int &hours, int &minute, int &seconds)
{
hours = amount/60;
amount %=60;
minute = amount/60;
amount %=60;
seconds = amount/120;
}
void Time::show ()
{
     std::cout <<"Hours"<<hour<<"Minutes"<<minute<<"Seconds"<<second<<endl;
}
int main() 
{ 
Time time1, time2, time3; 
time1.get(); 
time2.get(); 
time3.show(); 
system("pause"); 
return 0; 
}
I don't see a question.
Line 55: You're not going to change the class member called amount. In fact, you're just going to discard the value the user entered.

Line 17: Why is amount a member of the class?

Line 88: You might want to check your calculations here. If amount is specified in seconds, you could end up with some very large values to hours.

Line 88: This function is not a member of your class, even though you've declared a member function with the same signature.

Line 105: This is going to display zeroes. The default constructor for time3 will set it to zeroes.
Ok so I went back and simplified my code but it still seems to not be working.

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>
using namespace std;
class Time
{
      private:
              int amount, hour, minute, second;
              
              public:
                     void getdata ();
                     void displaydata ();
                     void setTime (int, int, int);
                     void change (int, int, int);
                     };
                     

void Time::setTime( int hour, int minute, int second ) 
{ 
     hour = amount/60;
     amount %=60;
     second = amount*60;
     minute = amount;
     }
     void Time::getdata ()
     {
       cout<<"Enter the amount of Time: "<<endl;
cin >> amount;
}   
 void Time::displaydata ();
{
    change(amount,hour,minute,second);
cout <<amount<<"is"<<hour<<"hour,"<<minute<<"minute"<<second<<"second";
{                                          
void change(int amount, int &hour, int &minute, int &second);
int main ()
system ("pause");
return 0;
Line 6: I still question why amount is a member of the class.

Line 18-21: Your calculations are still faulty. Also note that your assignment statements are changing the arguments named hour, minute, second, not the class members.

Is change supposed to be a member of your class or not? Line 12, you declare it as a member. Line 30, you call the member function, but I see no implementation of the member function. Line 30, your call to change does not match the number of arguments declared at line 12.

Line 32: Should be } not {

Line 33: This function prototype is useless. You have no function implementation matching that signature. The function prototype must be BEFORE any calls to the function.

Line 34: main needs {}
Line 34: main never instantiates Time.

Last edited on
Topic archived. No new replies allowed.