Listing 1 Date类的成员定义
// date.h: A simple date class
struct Date
{
int month;
int day;
int year;
Date * interval(const Date&);
};
/* End of File */
Listing 2 Date::interval()的实现
//date.cpp: Implement the Date class
#include "date.h"
inline int isleap(int y)
{return y%4 == 0 && y%100 != 0 || y%400 == 0;}
static int Dtab[2][13] =
{
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
Date * Date::interval(const Date& d2)
{
static Date result;
int months, days, years, prev_month;
// Compute the interval - assume d1 precedes d2
years = d2.year - year;
months = d2.month - month;
days = d2.day - day;
// Do obvious corrections (days before months!)
//
// This is a loop in case the previous month is
// February, and days < -28.
prev_month = d2.month - 1;
while (days < 0)
{
// Borrow from the previous month
if (prev_month == 0)
prev_month = 12;
--months;
days += Dtab[isleap(d2.year)][prev_month--];
}
if {months < 0)
{
// Borrow from the previous year
--years;
months += 12;
}
/* Prepare output */
result.month = months;
result.day = days;
result.year = years;
return &result;
}
/* End of File */
Listing 3 使用Date类的示例
// tdate.cpp: Test the Date class
#include <stdio.h>
#include <stdlib.h>
#include "date.h"
main()
{
Date d1, d2, *result;
int nargs;
// Read in two dates - assume 1st precedes 2nd
fputs("Enter a date, MM/DD/YY> ",stderr);
nargs = scanf("%d/%d/%d%*c", &d1.month,
&d1.day, &d1.year);
if (nargs != 3)
return EXIT_FAILURE;
fputs("Enter a later date, MM/DD/YY> ",stderr);
nargs = scanf("%d/%d/%d%*c", &d2.month,
&d2.day, &d2.year);
if (nargs != 3)
return EXIT_FAILURE;
// Compute interval in years, months, and days
result = d1.interval(d2);
printf("years: %d, months: %d, days: %d\n",
result->year, result->month, result->day);
return EXIT_SUCCESS;
}
/* Sample Execution:
Enter a date, MM/DD/YY> 10/1/51
Enter a later date, MM/DD/YY> 11/14/92
years: 41, months: 1, days: 13
*/
/* End of File */
Listing 4 Listing 5所示的变化后的Date类的定义
// date2.h
struct Date
{
int month;
int day;
int year;
// Constructors
Date();
Date(int, int, int);
Date * interval (const Date&);
};
/* End of File */
Listing 5 变化后的Date类
// date2.cpp
#include "date2.h"
inline int isleap(int y)
{return y%4 == O && y%100 != 0 || y%400 == 0;}
static int Dtab [2][13] =
{
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
Date * Date::interval (const Date& d2)
{
(same as in Listing 2)
}
Date::Date()
{
month = day = year = 0;
}
Date::Date(int m, int d, int y)
{
month = m;
day = d;
year = y;
}
/* End of File */
Listing 6 : Date类的测试程序
// tdate2.cpp
#include <stdio.h>
#include <stdlib.h>
#include "date2.h"
main()
{
int m, d, y, nargs;
// Read in two dates - assume 1st precedes 2nd
fputs("Enter a date, MM/DD/YY> ",stderr);
nargs = scanf("%d/%d/%d%*c", &m,&d,&y);
if (nargs != 3)
return EXIT_FAILURE;
Date d1(m,d,y);
fputs("Enter a later date, MM/DD/YY> ",stderr);
nargs = scanf("%d/%d/%d%*c", &m,&d,&y);
if (nargs != 3)
return EXIT_FAILURE;
Date d2(m,d,y);
// Compute interval in years, months, and days
Date *result = d1.interval(d2);
printf("years: %d, months: %d, days: %d\n",
result->year, result->month, result->day);
return EXIT_SUCCESS;
}
/* End of File */
Listing 7 将成员函数的实现放到类定义中去
// date2.h
struct Date
{
int month;
int day;
int year;
// Constructors
Date()
{month = day = year = 0;}
Date(int m, int d, int y)
{month = m; day = d; year = y;}
Date * interval(const Date&);
};
/* End of File */
Listing 8 使用存取函数获取变量值的类定义
// date3.h
struct Date
{
private:
int month;
int day;
int year;
public:
// Constructors
Date()
{month = day = year = 0;}
Date(int m, int d, int y)
{month = m; day = d; year = y;}
// Accessor Functions
int get_month() const
{return month;}
int get_day() const
{return day;}
int get_year() const
{return year;}
Date * interval(const Date&) const;
};
/* End of File */
Listing 9 用 class 代替 struct 的类定义
// date3.h
class Date
{
int month;
int day;
int year;
public:
// Constructors
Date()
{month = day = year = 0;}
Date(int m, int d, int y)
{month = m; day = d; year = y;}
// Accessor Functions
int get_month() const
{return month;}
int get_day() const
{return day;}
int get_year() const
{return year;}
Date * interval(const Date&) const;
};
/* End of File */
Listing 10 对第三种版本日期类的测试
// tdate3. cpp
#include <stdio.h>
#include <stdlib.h>
#include "date3.h"
main()
{
int m, d, y, nargs;
// Read in two dates - assume 1st precedes 2nd
fputs("Enter a date, MM/DD/YY> ",stderr);
nargs = scanf("%d/%d/%d%*c", &m,&d,&y);
if (nargs != 3)
return EXIT_FAILURE;
Date d1(m,d,y);
fputs("Enter a later date, MM/DD/YY> ",stderr);
nargs = scanf("%d/%d/%d%*c", &m,&d,&y);
if (nargs != 3)
return EXIT_FAILURE;
Date d2(m,d,y);
// Compute interval in years, months, and days
Date *result = d1.interval(d2);
printf("years: %d, months: %d, days: %d\n",
result->get_year (),
result->get_month (),
result->get_day ());
return EXIT_SUCCESS;
}
/* End of File */