COUNT IN C++
/*************************************************************/
/* OBJECT-ORIENTED PROGRAMMING */
/* Peter Coad / Jill Nicola */
/* Source Code Diskette */
/* (c)1993 Object International, Inc. All rights reserved. */
/*************************************************************/
/*************************************************************/
/* TEST1.CPP */
/*************************************************************/
#include
#ifndef COUNTS_H
#include "counts.h"
#endif
main()
{
// Create an integer count object with an initial value
// of 5.
Integer_Count intcount(5);
// Get its value. And display it.
cout << endl << "INTEGER Demo" << endl << endl;
cout << "Current value: " << intcount.get_value()
<< endl;
// Tell it to increment itself twice.
intcount.increment();
intcount.increment();
cout << "Current value: " << intcount.get_value()
<< endl;
// Tell it to dencrement itself three times.
intcount.decrement();
intcount.decrement();
intcount.decrement();
cout << "Current value: " << intcount.get_value()
<< endl;
// Tell it to reset itself.
intcount.reset();
cout << "Current value: " << intcount.get_value()
<< endl;
cout << endl << "Happy trails." << endl;
return 0;
}
/*************************************************************/
/* OBJECT-ORIENTED PROGRAMMING */
/* Peter Coad / Jill Nicola */
/* Source Code Diskette */
/* (c)1993 Object International, Inc. All rights reserved. */
/*************************************************************/
/*************************************************************/
/* COUNT3.H */
/*************************************************************/
#ifndef COUNT3_H
#define COUNT3_H
#ifndef DATE_H
#include "date.h"
#endif
const MIN_ASCII_VALUE = 32;
const MAX_ASCII_VALUE = 126;
const ASCII_a = 97;
template
class Count {
public:
// Constructors/destructors
Count() { }
virtual ~Count() { }
// Implementors
virtual void increment() = 0;
virtual void decrement() = 0;
void reset(){value = reset_value;}
// Accessors
Count_Type get_value() { return value; }
void set_value(Count_Type new_value) { value =
new_value; }
Count_Type get_reset_value() { return reset_value; }
void set_reset_value(Count_Type new_value) { value =
new_value; }
// Human interaction accessor
Count_Type get_display_value() { return value; }
protected:
// Data members
Count_Type value;
Count_Type reset_value;
};
class Integer_Count : public Count {
public:
// Constructors/destructors
Integer_Count() { reset_value = 0; reset(); }
Integer_Count(int new_value)
{ reset_value = 0; value = new_value; }
~Integer_Count() { }
// Implementors
void increment();
void decrement();
char* asBase(int number_base);
};
class ASCII_Count : public Count {
public:
// Constructors/destructors
ASCII_Count() { reset_value = 'a'; reset(); }
ASCII_Count(char new_char) { reset_value = 'a';
value = new_char; }
~ASCII_Count() { }
// Implementors
void increment();
void decrement();
};
class Date_Count : public Count {
public:
// Constructors/destructors
Date_Count() { this->reset(); }
Date_Count(Date& new_date) {reset_value = new_date;
this->reset(); }
~Date_Count() { }
// Implementors
void increment();
void decrement();
// Helper member functions
char* get_display_value();
};
#endif
/*************************************************************/
/* OBJECT-ORIENTED PROGRAMMING */
/* Peter Coad / Jill Nicola */
/* Source Code Diskette */
/* (c)1993 Object International, Inc. All rights reserved. */
/*************************************************************/
/*************************************************************/
/* COUNT3.CPP */
/*************************************************************/
#include
#include
#include
#include
#include
#ifndef COUNT3_H
#include "count3.h"
#endif
// Integer_Count member functions
void Integer_Count::increment()
{
value++;
}
void Integer_Count::decrement()
{
value--;
}
char* Integer_Count::asBase(int new_base)
{
char answer_string[80];
char partial_answer_string[80];
char remainder_string[10];
int quotient;
int remainder;
remainder = value % new_base;
quotient = quotient / new_base;
itoa(remainder, remainder_string, new_base);
strcpy(answer_string, remainder_string);
while(quotient != 0)
{
remainder = quotient % new_base;
quotient = quotient / new_base;
itoa(remainder, remainder_string, new_base);
strcpy(partial_answer_string, remainder_string);
strcat(partial_answer_string, answer_string);
strcpy(answer_string, partial_answer_string);
}
return answer_string;
}
// ASCII_Count member functions
void ASCII_Count::increment()
{
value++;
// Handle wraparound
if (toascii(value) > MAX_ASCII_VALUE)
value = (char) MIN_ASCII_VALUE;
}
void ASCII_Count::decrement()
{
value--;
// Handle wraparound
if (toascii(value) < MIN_ASCII_VALUE)
value = (char) MAX_ASCII_VALUE;
}
// Date Count member functions
void Date_Count::increment()
{
value++;
}
void Date_Count::decrement()
{
value--;
}
char* Date_Count::get_display_value()
{
return value.print_string();
}
/*************************************************************/
/* OBJECT-ORIENTED PROGRAMMING */
/* Peter Coad / Jill Nicola */
/* Source Code Diskette */
/* (c)1993 Object International, Inc. All rights reserved. */
/*************************************************************/
/*************************************************************/
/* DATE.H */
/*************************************************************/
#include
#include
#ifndef DATE_H
#define DATE_H
enum Weekday { sun = 1, mon = 2, tue = 3, wed = 4,
thu = 5, fri = 6, sat = 7 };
class Date {
public:
// Constructors/destructors
Date();
Date(int day, int month, int year);
~Date() { }
// Implementors
void set_date(int day, int month, int year);
char* print_string();
Weekday day_of_week();
int day_of_month();
int month();
int year();
// Operators
Date operator++(); // prefix
Date operator++(int); // postfix
Date operator--(); // prefix
Date operator--(int); // postfix
friend int operator< (Date& date1, Date& date2);
friend int operator> (Date& date1, Date& date2);
friend int operator== (Date& date1, Date& date2);
friend ostream& operator<< (ostream& os, Date& a_date);
private:
time_t calendar_time;
static long seconds_per_day;
};
#endif
/*************************************************************/
/* OBJECT-ORIENTED PROGRAMMING */
/* Peter Coad / Jill Nicola */
/* Source Code Diskette */
/* (c)1993 Object International, Inc. All rights reserved. */
/*************************************************************/
/* DATE.CPP */
/*************************************************************/
#include
#include
#ifndef DATE_H
#include "date.h"
#endif
long Date::seconds_per_day = 86400;
Date::Date()
{
calendar_time = time(NULL);
}
Date::Date(int day, int month, int year)
{
this->set_date(day, month, year);
}
Weekday Date::day_of_week()
{
struct tm* time_struct = localtime(&calendar_time);
return (Weekday)time_struct->tm_wday;
}
int Date::day_of_month()
{
struct tm* time_struct = localtime(&calendar_time);
return time_struct->tm_mday;
}
int Date::year()
{
struct tm* time_struct = localtime(&calendar_time);
return time_struct->tm_year;
}
int Date::month()
{
// Months range from 0 (January) to 11 (December)
struct tm* time_struct = localtime(&calendar_time);
return time_struct->tm_mon + 1;
}
void Date::set_date(int day, int month, int year)
{
struct tm time_struct = {0};
time_t temp_time;
time_struct.tm_mday = day;
// Months range from 0 (January) to 11 (December)
time_struct.tm_mon = month - 1;
time_struct.tm_year = year;
temp_time = mktime(&time_struct);
if (temp_time != (time_t)-1)
calendar_time = temp_time;
}
char* Date::print_string()
{
char* date_string = new char[30];
struct tm* time_struct = localtime(&calendar_time);
strftime(date_string, 30, "%x", time_struct);
return date_string;
}
ostream& operator<< (ostream& os, Date& aDate)
{
char* date_string = aDate.print_string();
os << date_string;
return os;
}
Date Date::operator++()
{
calendar_time += seconds_per_day;
return *this;
}
Date Date::operator++(int)
{
calendar_time += seconds_per_day;
return *this;
}
Date Date::operator--()
{
calendar_time -= seconds_per_day;
return *this;
}
Date Date::operator--(int)
{
calendar_time -= seconds_per_day;
return *this;
}
int operator< (Date& date1, Date& date2)
{
return date1.calendar_time < date2.calendar_time;
}
int operator> (Date& date1, Date& date2)
{
return date1.calendar_time > date2.calendar_time;
}
int operator== (Date& date1, Date& date2)
{
return date1.calendar_time == date2.calendar_time;
}