Difference between revisions of "C++ programming/Chapter 12/TimeOfDay example"

From Teknologisk videncenter
Jump to: navigation, search
m (Running)
m (Download)
Line 200: Line 200:
 
</source>
 
</source>
 
=Download=
 
=Download=
*[http:mars.tekkom.dk/code/TimeOfDay.tar.gz TimeOfDay.tar.gz]
+
*[http://mars.tekkom.dk/code/TimeOfDay.tar.gz TimeOfDay.tar.gz]
 
[[Category:c++]]
 
[[Category:c++]]

Revision as of 06:34, 17 January 2011

TimeOfDay.h

class TimeOfDay
{
public:
  TimeOfDay();
  TimeOfDay(int hours, int minutes, int seconds);
  TimeOfDay Increment() const;
  void Write() const;
  bool Equal(TimeOfDay otherTime) const;
  bool LessThan(TimeOfDay otherTime) const;
private:
  int hours;
  int minutes;
  int seconds;
};

TimeOfDay.cpp

//******************************************************************
// IMPLEMENTATION FILE (TimeOfDay.cpp)
// This file implements the TimeOfDay member functions
//******************************************************************
#include "TimeOfDay.h"
#include <iostream>
 
using namespace std;

TimeOfDay::TimeOfDay() 
{
  hours = 0;
  minutes = 0;
  seconds = 0;
}

//******************************************************************

TimeOfDay::TimeOfDay(int initHours, int initMinutes, int initSeconds)
{
  hours = initHours;
  minutes = initMinutes;
  seconds = initSeconds;
}
 
//******************************************************************

TimeOfDay TimeOfDay::Increment() const
{
  // Create a duplicate of instance and increment duplicate
  TimeOfDay result(hours, minutes, seconds);
  result.seconds = result.seconds++;  // Increment seconds
  if (result.seconds > 59)            // Adjust if seconds carry
  {
    result.seconds = 0;
    result.minutes = result.minutes++;
    if (result.minutes > 59)          // Adjust if minutes carry
    {
      result.minutes = 0;
      result.hours = result.hours++;
      if (result.hours > 23)          // Adjust if hours carry
        result.hours = 0;
    }
  }

  return result;
}
 
//******************************************************************
 
void TimeOfDay::Write() const
{
  // Insert extra 0 if there is only one digit in a place
  if (hours < 10)
    cout << '0';
  cout << hours << ':';
  if (minutes < 10)
    cout << '0';
  cout << minutes << ':';
  if (seconds < 10)
    cout << '0';
  cout << seconds;
}
 
//******************************************************************
 
bool TimeOfDay::Equal(TimeOfDay otherTime) const
{
  return (hours == otherTime.hours 
          && minutes == otherTime.minutes 
          && seconds == otherTime.seconds);
}
 
//******************************************************************
 
bool TimeOfDay::LessThan(TimeOfDay otherTime) const
{
  return (hours < otherTime.hours || hours == otherTime.hours 
          && minutes < otherTime.minutes || hours == otherTime.hours 
          && minutes == otherTime.minutes
          && seconds < otherTime.seconds);
}

testtimeofday.cpp

//******************************************
// A program to create two time objects 
// and manipulate them.
//******************************************
#include <iostream>
#include "TimeOfDay.h"        // For TimeOfDay class
using namespace std;
 
int main()
{
  TimeOfDay time1(5, 30, 0);  // Instantiate two TimeOfDay objects
  TimeOfDay time2;
  int  loopCount;
 
  cout << "time1: ";          // Print them and compare them
  time1.Write();
  cout << "  time2: ";
  time2.Write();
  cout << endl;
  if (time1.Equal(time2))
    cout << "Times are equal" << endl;
  else
    cout << "Times are NOT equal" << endl;
 
  time2 = time1;              // Set them equal

  cout << "time1: ";          // Print them and compare them
  time1.Write();
  cout << "  time2: ";
  time2.Write();
  cout << endl;
 
  if (time1.Equal(time2))
    cout << "Times are equal" << endl;
  else
    cout << "Times are NOT equal" << endl;
 
  time2.Increment();          // Increment one, print and compare
  cout << "New time2: ";
  time2.Write();
  cout << endl;
  if (time1.LessThan(time2))
    cout << "time1 is less than time2" << endl;
  else
    cout << "time1 is NOT less than time2" << endl;
 
  if (time2.LessThan(time1))
    cout << "time2 is less than time1" << endl;
  else
    cout << "time2 is NOT less than time1" << endl;
 
  TimeOfDay time4(23, 59, 55);   // Instantiate one near the maximum
  cout << "Incrementing time1 from 23:59:55:" << endl;
  for (loopCount = 1; loopCount <= 10; loopCount++)
  {
    time4.Write();
    cout << ' ';
    time4 = time4.Increment();   // Check that it overflows properly
  }
  cout << endl;
  return 0;
}

Makefile

CC=g++
CFLAGS=-Wall

TIMEOFDAYFILES=Makefile TimeOfDay.h TimeOfDay.cpp testtimeofday.cpp
TIMEOFDAYOBJECTS=TimeOfDay.o testtimeofday.o

testtimeofday: $(TIMEOFDAYOBJECTS)
        $(CC) $(FLAGS) $(TIMEOFDAYOBJECTS) -o testtimeifday

Compiling

[heth@mars ~/cp/TimeOfDay]$ make
c++ -Wall -c TimeOfDay.cpp
c++ -Wall -c testtimeofday.cpp
g++  TimeOfDay.o testtimeofday.o -o testtimeifday

Running

[heth@mars ~/cp/TimeOfDay]$ ./testtimeifday
time1: 05:30:00  time2: 00:00:00
Times are NOT equal
time1: 05:30:00  time2: 05:30:00
Times are equal
New time2: 05:30:00
time1 is NOT less than time2
time2 is NOT less than time1
Incrementing time1 from 23:59:55:
23:59:55 23:59:56 23:59:57 23:59:58 23:59:59 00:00:00 00:00:01 00:00:02 00:00:03 00:00:04

Download