Please send questions to st10@humboldt.edu .

*   CIS 130 - Week 11 Lecture projected notes - 4-2-07

*   (modified a bit before posting, 4-4-07)

*   both Python and C++ are high-level languages ---
    the computer cannot run them directly, they have
    to be translated into machine-level commands

TWO of the major ways to translating high-level languages:
*   interpreting
*   compiling

    *   (and there are interesting agglomerations of these two as
        well!)

*   interpreting: translate ONE LINE at a time, and execute
    it;

    ...like in the python interpreter: you type in a line at
    the >>>, it is translated and executed.

    (if you type the same line 10 times, it would be translated
    and executed 10 times)

*   compiling: translate a whole FILE of commands at a time,
    resulting in a file that's at a "lower" level

    ...for C++, usually ready to be directly executed by
    the machine.

*   two short-term implications to note:

    *   compiling is all-or-nothing --- you either get a
        complete translated result, or you get NOTHING

    *   once you have successfully compiled something,
        you can run it as many times as you want without
        any re-translating being necessary (so those
        later runs are faster)

*   (we drew the classic compiling picture on the board)

*   The C++ compiler program on cs-server: g++
    
*   you save Python functions in a Python module, a file
    whose name ends in the suffix .py

    ...you save C++ functions in a C++ source code file,
    a file whose name ends in the suffic .cpp

*   NOW let's get to C++ syntax, and how it compares to Python

*   Integer literals - the SAME in C++ and Python:

    13   
    -27
    20000
    +23

*   Floating point literals - the SAME in C++ and Python, too!

    1.2
    -1.2
    +2.3
    1.3e27

*   how about Strings? in Python, there's at least 3
    ways to write a string --- single quotes, double quotes,
    triply-quoted, and maybe more ---

    in C++, there is only ONE way to write a string literal:

    ...in double quotes

    "Hello, I am a C++ string literal"

*   Python doesn't have a separate character literal,
    but C++ does:

    ...a character between single quotes

    'a'   <---is a C++ character literal
    "a"   <---is a C++ string

    '\n'   <--- is the newline character in C++
    "\n"   <--- is a string containing a newline character in C++

*   The Python bool literals are True and False;
    the C++ bool literals are true and false

    (but the C++ true occasionally appears as integer 1,
     and the C++ false occasionally appears as integer 0

*   what about expressions?

    *   we have + - * / in both C++ and Python

    *   ...similar operator precedence, also, BUT when in
        doubt, USE PARENTHESES!!!!!

    *   and look, % (modulo) is there in both Python and C++
        also

        (modulo, %, is the integer remainder of integer
        division)

    *   yes, that includes integer division
        (3 / 5  is 0 in both Python and C++)

    *   but if either operand is floating point, the
        operation will also be floating point in both Python
        and C++

    *   we still have > < >= <= != ==

        (and = is ASSIGNMENT in both, also)

    *   the Boolean operators   and  or   not   in Python
	are:                    &&   ||   !     in C++

        (beware: & and | are C++ operators, but they are
         bitwise operations beyond the scope of CIS 130)

*   SO: now let's get to some bigger differences:

    *   in Python --- do you want a variable blah?

        ...then just assign a value to it:

	blah = 3

    *   in C++ --- you have to DECLARE a name before you
        can ASSIGN to it ---

        in particular, you have to DEFINE a variable
        to be of a certain type, and THEN you can give it
        a value.

        int blah;
        blah = 3;

        int woof = 5;

        ...oh yes --- and a SEMICOLON terminates a C++ STATEMENT


*   here are some C++ types:

    int   --> integer
    
    double --> double-precision floating point
    float  --> single-precision floating point

    ...and usually we just use double for C++ floating
       point, unless space/memory is a big concern

    char  --> character

    string  --> "modern" string type in C++
    char*   --> "old" string type in C++

    bool    --> boolean type in C++


*   this extends to functions, too ---

    functions must have a RETURN TYPE specified 
    (the TYPE of the value it RETURNS)
    ---> if a function does not return anything, it
         has a return type of void

    ...and each PARAMETER's type must be specified.

*   COMMENT in Python:   # comment
    2 comment styles in C++:

    // single line comment

    /* a
       multi-line
       comment */

*   compare function headers in Python and C++:

    def circ_area(radius): # Python

    double circ_area(double radius)  // in C++

   
    def ring_area(outer_radius, hole_radius): # Python

    double ring_area(double outer_radius, double inner_radius) //C++


    def print_greeting(): # Python
 
    void print_greeting()   // C++

    SO, the C++ function header syntax is:

    <funct_type> <funct_name>( <param type> <param name>,
                               <param type> <param name>, ... )

*   what about the function body?

    in Python, everything indented after the def line is part
    of the function body ---

    in C++, everything within the {  }  after the function header
    is part of the function body.

    def circ_area(radius):
        return 3.14159 * (radius * radius)


    double circ_area(double radius)
    {
        return 3.14159 * (radius * radius);
    }

    NOW: does it matter where you put the { } and the body in
    C++?
    ... to the compiler? NO. It is not syntactically significant.

    BUT!!!! I care a great deal about indentation and white
    space to enhance readability...

    CIS 130 STYLE RULES:
    1. { and } on their own lines (as shown above)
    2. thou SHALT indent the statements within { and } at
       least 3 spaces

*   notice that the return statement in C++ is similar to the
    Python one:

    return <expression>;

                       ^ except it ends with a semicolon

*   in Python, you can just import and execute a function;

    ...in C++, you run a C++ program, which consists of as
    many functions as you want, one of which is named main()

    ...when you execute the program, main() starts up.

    ,...but we'll use funct_play2 to play with C++ functions
    BEFORE dealing with main().

*   ONE MORE THING... (mentioned in LAB 4-4-07):

    to declare a NAMED CONSTANT in C++:

    const <c_type> <C_NAME> = <expr>;

    const double PI = 3.14159;
    const int SMALL_LIMIT = 5;