Mihai's CS368-2 (Fall 1999):
C++ for Java Programmers

The Computer Science 368-2 course covers C++ and is for students with previous Java experience. This page is not the main class page. The main class page used to be located at:

http://www.cs.wisc.edu/~cs368-2/
Please refer to that page before asking any questions.

Below you will find questions and answers related to the on-line lessons, the homework, and the programming assignments. The questions are listed by date, with the most recent at the top. Please check the whole list of questions before contacting the TA or the professor. Thanks.

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).
  • for functions:
    If functions A, B, and C, in file1.C are called from file2.C, create a header file named file1.h, put the declarations of A, B, and C in file1.h, and #include "file1.h" in file2.C.
  • for structures (struct, union, class):
    If the structure is used by functions in file1.C and file2.C, create a header file named common.h, put the definition of the structure in common.h, and #include "common.h" in both file1.C and file2.C.
  • for constants (const int, const double, ...):
    Similar to structures.
Obviously, the rules above can be extended to more than two files.

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:
  1. put your solution file(s) in your CS private directory (~/private/)

    If your solution file(s) is/are on a computer outside the CSL lab (your home computer, for example), you can FTP the files to your CS private directory. For more information on that please check the CSL page http://www.cs.wisc.edu/csl/doc/howto/remote/index.html.

    If you create your solution files on an NT CSL computer, save the files in the U:\private directory.

    If you create your solution files on an Unix CSL computer, save the files in your private directory (~/private/).

  2. open a terminal on one of the nova computers

    You can do that remotely, by ssh or telnet, or by logging in to any of the nova machines in the CSL Unix lab.

    If you are on the nova machines in the CSL Unix lab, open an xterm.

  3. make sure you are in your private directory

    Type the following at the terminal command line:

    % cd ~/private/

    (note: % is part of the prompt, you do not have to type it)

  4. copy the solution file(s) to the turnin directory

    Type the following at the terminal command line:

    % cp <files> <turn-in path>

  5. check whether the files were copied

    Type the following at the terminal command line:

    % ls <turn-in path>

    You should see the solution file(s) required by the assignment.

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 "~cs368-2/public/SECTION2/turnin/HW1/<your login name>". If I were to turn in my HW1 solution, I would use the path ~cs368-2/public/SECTION2/turnin/HW1/mihai.

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.

Copyright 1998-2005 Mihai Christodorescu. All rights reserved.
Maintained by Mihai Christodorescu (http://mihai.christodorescu.org).
Created: Mon Dec 21 21:12:13 PST 1998
Last modified: Sat Oct 1 23:06:21 CDT 2005