Mihai's CS368-2 (Fall 1999):
|
|
Mon Oct 25, 1999 Q:Why do we need header files? What do I put in header files? |
| A:
Header files allow you to share common information among various source files. For a function, we define the function declaration as the return type, the name of the function, and the list of arguments. The function definition is the function declaration followed by the function body. For example:
// function declaration<br />
double compute_root( double a, double b, double c );
// function definition
double compute_root( double a, double b, double c )
{
double delta = b * b - 4 * a * c;
if( delta < 0 )
{
cout << "Error!" << endl;
return 0.0;
}
return ( - b + sqrt( delta ) ) / ( 2 * a );
}
Obviously, the declaration and the definition have to match
for the same function!Suppose you create some functions called A, B, and C and you put them in the same source file called myfuncs.C. Let's suppose that the function A needs to call another small function called A1. We can put A1 in myfuncs.C also. In C++ (and C), the functions A, B, C, and A1 are only known inside the file myfuncs.C. Inside that file, any function (A, B, C, or A1) can call any other function. If you write a main function and put it in a file main.C, it will not know about the functions A, B, C, or A1. If you try to call A (or B, or C, or A1) from main, you will get a compile-time error, because the compiler does not know about those functions when it reads the main.C file. To fix this, we have to somehow have main.C know about the functions it uses. The easiest way to do this is to put the function definitions for the function used by main inside the main.C file. So we can do this for every file that uses the functions A, B, or C. This is one solution, but it is incomplete. What happens if we add one parameter to the arguments of the function A? Then we have to go and modify all the files that use the function A to know about the new parameter! This is cumbersome and prone to error. We can just put the function declarations in a header file named myfuncs.h (so we know it contains declarations for functions in myfuncs.C) and #include myfuncs.h in each source file that uses those functions. This way, we just need to modify the header file myfuncs.h and the source file myfuncs.C whenever we change the function arguments. So header files (.h) help us share common information among source files (.C).
Keep in mind that these rules are not part of the language, the compiler does not enforce them, but they are standard programming practice. |
|
Wed Oct 13, 1999 Q:I cannot turn in my homework. It says "permission denied." Help! |
| A:
Here is what you need to do:
That's it! <files> is the list of files you have to turn in, named as required by the assignment. For example, for Written Assignment #1, there was only one file to turn in, named "hw1". <turn-in path> is the name of the directory you have to turn in your files into, as specified by the assignment. For example, for Written Assignment #1, the turn-in path was |
|
Thu Oct 7, 1999 Q:I took some Computer Science classes a while ago. Now, whatever accounts I had there are expired. But in order to use the computers at CS for this course, I need to have a login... so what should I do? |
| A:
Your account with the CS department should have been reactivated. Try logging in to one of the machines in the CS labs. If that does not work, or if you forgot your password, please go to the CSL office, 2350 Computer Sciences and Statistics, and have your account activated ASAP. |