Using Member Functions!!

Sep 27, 2009 at 3:27am
This time class is driving me crazy...How can I get this program to calculate how long I have to wait in hours, mins and seconds for lunch and dinner using 'diff' ??????

#include "time.h"
int main()
{
Time begin;
Time end;
Time check_in;


int h, m, s;
begin.set (12,0, 0);
end.set (18, 0, 0);

cout << " enter hour, min, sec\n ";
cin >>h>>m>>s;
check_in.set (h, m, s);

cout << " the time you checked in ";
check_in.print ();

if (check_in.Equal(begin))
cout << "You are right on time. Lets go to lunch !! ";

if( check_in.LessThan(begin))
cout << " Good Morning you are early. You have to wait for lunch !! ";

if( check_in.GreaterThan(begin))
cout << "Good Afternoon you are late. You have to wait for dinner !! ";

return 0;
}
Sep 27, 2009 at 4:07am
Huh??? There's no Time class in time.h. Furthermore, there's no Time class defined anywhere in the C++ standard. Where did you get the idea that there was one?
Sep 27, 2009 at 2:30pm
Well here is the rest of the program that I previous omitted...

#include <iostream> // Time.h

using namespace std;

class Time // DECLARATION
{
public:
void set(int h, int m, int s);
void print ();
void increment();
int get_hour();
int get_mins();
int get_secs();
bool Equal(Time t);
bool LessThan(Time t);
bool GreaterThan(Time t);

private: // The private variables hrs, mins, and secs can be accessed only by the member functions Set, Increment, Write, Equal, and LessThan, not by client code.
int hour;
int mins; // MEMBERS VARIABLES OF TIME
int secs;
};

#include "time.h"

void Time::set(int h, int m, int s)
{
hour = h;
mins = m;
secs = s;
}
void Time::increment()
{
secs ++;
if (secs == 60)
{ mins ++;
secs = 0;
if ( mins == 60)
{ hour ++;
mins = 0;
}
if ( hour == 24)
hour = 0;
}
}

void Time::print()
{
if (hour < 10) cout << '0';
cout << hour << ":";
if (mins < 10) cout << '0';
cout << mins << ":";
if (secs < 10) cout << '0';
cout << secs << endl;

}

int Time::get_hour()
{ return hour;
}
int Time::get_mins()
{ return mins;
}
int Time::get_secs()
{ return secs;
}

bool Time::Equal(Time t)
{
if ( hour == t.hour && mins == t.mins && secs == t.secs)
return true;
else
return false;
}

bool Time::LessThan(Time t)
{
if(hour < t.hour || hour == t.hour && mins < t.mins || hour == t.hour && mins == t.mins && secs < t.secs)
return true;
else
return false;
}

bool Time::GreaterThan(Time t)
{
if(hour > t.hour || hour == t.hour && mins > t.mins || hour == t.hour && mins == t.mins && secs > t.secs)
return true;
else
return false;
}
Sep 27, 2009 at 2:38pm
Oh, sorry. I read the #include "time.h" as #include <time.h>.

So what's the problem? It doesn't compile? It gives wrong results?
Sep 27, 2009 at 2:45pm
I'm trying to calculate the difference between the check_in time and the begin.set time....but I also have to include the 'diff' utility( including diff.gethour(), ....etc)...Can you help me???
Topic archived. No new replies allowed.