I l@ve RuBoard |
![]() ![]() |
2.9 Setting Up a Header FileBefore I can invoke seq_elem(), I must first declare it to the program. If it is invoked in five program text files, there must be five declarations available. Rather than separately declare seq_elem() in each of the five files, we place the function declaration in a header file. The header file is then included in each program text file that wishes to use the function. Using this convention, we need maintain only a single declaration of a function. If its parameter list or return type changes, only this one declaration needs to be modified. All users of the function automatically include the updated function declaration. Header files by convention are given a .h suffix ?except for the standard library header files, which have no suffix. We'll call our header file NumSeq.h and place in it a declaration of all the functions related to our numeric sequences: // NumSeq.h bool seq_elem( int pos, int &elem ); const vector<int> *fibon_seq( int size ); const vector<int> *lucas_seq( int size ); const vector<int> *pell_seq( int size ); const vector<int> *triang_seq( int size ); const vector<int> *square_seq( int size ); const vector<int> *pent_seq( int size ); // and so on ... There can be only one definition of a function in a program. However, there can be multiple declarations. We don't put definitions in a header file because the header file is included in multiple text files within a program. One exception to this one-definition rule is the definition of an inline function. To expand an inline function, the definition must be available to the compiler at each invocation point. This means that we must place the inline function definitions inside a header file rather than in a separate program text file. Objects defined at file scope are also declared in a header file if multiple files may need to access the objects. This is because an object can not be referred to until it has been declared to the program. For example, if seq_array were defined at file scope, we would likely provide a declaration within NumSeq.h. Not unexpectedly, our first try is not quite correct: // This is not quite right ... const int seq_cnt = 6; const vector<int>* (*seq_array[seq_cnt])( int ); This is not correct because it is interpreted as the definition of seq_array and not as a declaration. Just as with a function, an object can be defined only once in a program. The definition of an object, as well as the definition of a function, must be placed in a program text file. We turn the definition of seq_array into a declaration by prefacing it with the keyword extern: // OK: this is a declaration extern const vector<int>* (*seq_array[seq_cnt])( int ); OK, granted, you might say. This is analogous to the distinction of placing function declarations in the header file but function definitions in a program text file. But. But. (You're on your feet by this point.) But if all this is true, then isn't seq_cnt also declared extern rather than explicitly defined? Obviously, this is just to confuse you. Me. The whole gaggle of us. A const object, like an inline function, is treated as an exception to the rule. The definition of a const object is not visible outside the file it is defined in. This means that we can define it in multiple program files without error. Why do we want that? It is because we want the compiler to use the value of the const object in our array declarations and other situations in which a constant expression is needed, possibly across multiple files. A file using either seq_elem() or seq_array includes the header file before a first use of either name: #include "NumSeq.h" void test_it() { int elem = 0; if ( seq_elem( 1, elem ) && elem == 1 ) // ... } Why is NumSeq.h in quotation marks rather than angle brackets (<,>)? The short answer is that if the header file is in the same directory as the program text file including it, we use quotation marks. If it is anywhere else, we use angle brackets. The slightly more technical answer is that if the file name is enclosed by angle brackets, the file is presumed to be a project or standard header file. The search to find it examines a predefined set of locations. If the file name is enclosed by a pair of quotation marks, the file is presumed to be a user-supplied header file. The search to find it begins in the directory in which the including file is located. Exercise 2.2The formula for the Pentagonal numeric sequence is Pn=n*(3n-1)/2. This yields the sequence 1, 5, 12, 22, 35, and so on. Define a function to fill a vector of elements passed in to the function calculated to some user-specified position. Be sure to verify that the position specified is valid. Write a second function that, given a vector, displays its elements. It should take a second parameter identifying the type of numeric series the vector represents. Write a main() function to exercise these functions. Exercise 2.3Separate the function to calculate the Pentagonal numeric sequence implemented in Exercise 2.2 into two functions. One function should be inline; it checks the validity of the position. A valid position not as yet calculated causes the function to invoke a second function that does the actual calculation. Exercise 2.4Introduce a static local vector to hold the elements of your Pentagonal series. This function returns a const pointer to the vector. It accepts a position by which to grow the vector if the vector is not as yet that size. Implement a second function that, given a position, returns the element at that position. Write a main() function to exercise these functions. Exercise 2.5Implement an overloaded set of max() functions to accept (a) two integers, (b) two floats, (c) two strings, (d) a vector of integers, (e) a vector of floats, (f) a vector of strings, (g) an array of integers and an integer indicating the size of the array, (h) an array of floats and an integer indicating the size of the array, and (i) an array of strings and an integer indicating the size of the array. Again, write a main() function to exercise these functions. 2.6 Exercise 2.6Reimplement the functions of Exercise 2.5 using templates. Modify the main() function accordingly. ![]() |
I l@ve RuBoard |
![]() ![]() |