Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ Timesaving Techniques (2005) [eng].pdf
Скачиваний:
65
Добавлен:
16.08.2013
Размер:
8.35 Mб
Скачать

Testing the cDate Class

35

LISTING 5-2: NON-INLINE METHODS

bool cDate::IsValidDate( int day, int month, long year )

{

// Is the month valid?

if ( month < 0 || month > 11 ) return false;

// Is the year valid?

if ( year < 0 || year > 9999 ) return false;

//Is the number of days valid for this month/year? if ( day < 0 || day > MonthDays(month, year) )

return false;

//Must be ok

return true;

}

 

bool cDate::IsLeapYear( long year )

 

{

 

int year_end = year % 100;

// year in century

if (((year%4) == 0) && ((year_end !=0) || ((year%400) == 0))) return true;

return false;

}

Putting this code into a single object and sharing that code among various projects that might need this functionality offers some obvious advantages:

If the code needs to be changed, for example, to account for some bug in the leap year calculation, this change can all be done in one place.

More importantly, if changes are made to implement a newer, faster way to calculate the leap year or the day of the week, or even to add functionality, none of those changes affect the calling programs in the least. They will still work with the interface as it stands now.

Testing the cDate Class

After you create a class, it is important to create a test driver — doing so not only ensures that your code is correct, but also shows people how to use your code.

1. In the code editor of your choice, reopen the source file to hold the code for your test program.

In this example, I named the test program ch1_5.cpp.

2. Type the code from Listing 5-3 into your file.

Better yet, copy the code from the source file on this book’s companion Web site.

36 Technique 5: Separating Rules and Data from Code

LISTING 5-3: THE CDATE CLASS TEST PROGRAM

#include <iostream> using namespace std;

int main(int argc, char **argv)

{

//Do some testing. First, a valid date cDate d1(31, 11, 2004);

//Now, an invalid one.

cDate d2(31, 12, 2004);

//Finally, let’s just create a blank one. cDate d3;

//Print them out

cout << “D1: “ << “Month: “ << d1.Month() << “ Day: “ << d1.Day() << “ Year: “ << d1.Year() << endl;

cout << “D2: “ << “Month: “ << d2.Month() << “ Day: “ << d2.Day() << “ Year: “ << d2.Year() << endl;

cout << “D3: “ << “Month: “ << d3.Month() << “ Day: “ << d3.Day() << “ Year: “ << d3.Year() << endl;

return 0;

}

3. Save the source code as a file in your code editor and close the editor application.

4. Compile the source code with your favorite compiler on your favorite operating system.

5. Run the program on your favorite operating system console.

If you have done everything properly, you should see the following output from the program on the console window:

$ ./a.exe

D1: Month: 11 Day: 31 Year: 2004 D2: Month: 2011708128 Day: -1 Year:

2011671585

D3: Month: 8 Day: 7 Year: 2004

Note that the numbers shown in the output may be different on your computer, because they are somewhat random. You should simply expect to see very invalid values.

This, then, is the advantage to working with objectoriented programming in C++: You can make changes “behind the scenes” without interfering with the work of others. You make it possible for people to get access to data and algorithms without having to struggle with how they’re stored or implemented. Finally, you can fix or extend the implementations

of your algorithms without requiring your users to change all their applications that use those algorithms.

Part II

Working with the Pre-Processor