February 11, 2002 Reading assignment: Deitel and Deitel - read section 5.11, read the rest of chapter 6 -or- Wang p 157-159, 4.4-4.5, 5.4 We continue/complete the example from last time. - Relevant files from last time are ratnum.h, ratnum.cpp, testrat.cpp - Notice that this header file uses #ifndef, #endif, and #define. This stops the header for inadvertantly being included more than once. This would happen in larger programs in which headers must include other headers. - If you have a function that you need as part of your class implementation, but you want it just as an internal function, then define it as private. Such functions are called utility functions. Other operators - The other operators, like +, -, ==, +=, !, etc. are not defined automatically. If you want to, you can make them part of your class. We'll learn this in Chapter 8. Function pointers and arrays of functions (5.11 of D&D, 157-159 in Wang) - A function is actually just a pointer to the memory location where its code is stored - Since a function is really just a pointer to its code, you can define arrays of functions. - Example: int average(int x, int y) { // Here's a function definition return (x+y)/2; } int (* func)(int, int); // func is a pointer to a function that takes // two integer parameters and returns an int func = &average; // Now func points to the function "average" // acceptable shorthand is func = average; cout << (*func)(27, 29); // This calls the function "average" // outputs "28". Acceptable shorthand is // func(27,29) - This is useful, for example, if you have a few similar functions to call. These functions must have the same return type and same parameter list void straight_flush(Card *); // one function void four_of_a_kind(Card *); // another function void full_house(Card *); // a third function void (* hand_evaluation[3])(Card *) = {&straight_flush, &four_of_a_kind, &full_house}; for (i = 0; i < 3; i++) ((*hand_evaluation)[i])(myhand); // acceptable shorthand for the last line is hand_evaluation[i](myhand);