Please send questions to st10@humboldt.edu .

*   CIS 130 - Week 7 - Wednesday, March 3, 2010

*   Now: on to C++!

*   C++ simple expressions

    *   like Scheme: a literal is a simple expression, and
        a name that's been given a value is a simple expression;

    *   most expressions have a type, and THOSE differ a little in
        C++:

        instead of number, it has SEVERAL numeric types;
	we care about: int for integer
	               double for double-precision floating point

        (there are several others, but these 2 are fine for CIS 130...)

        IF you write a literal with JUST digits, and no decimal
	point (starting with a - or + is OK), that's an int

	3
	-15
	105
	+13

        If you stick a decimal in amongst those digits (leading + or
	- is still OK), you get a literal of type double:

        3.5
        3.0 
        -15.00001
        +22.5

        1E7   <-- that 1 * 10 to the 7th power

        C++ has 2 string types --
	the more modern type string
	and the older char*

	...I'm only mentioning the older string type
	because so-called string literals, anything in double quotes,
	is of that older type char*

	"hi"   <-- literal of type char*
	(but we'll tend to write functions that use type string)

        there's also a char type -  for character -
	and you write a char literal by surrounding (typically)
	    ONE character in single quotes

	'a' <--- of type char
	"a" <--- of type char*

	special exceptions:
	'\n' -- that's a newline character literal
	'\t' -- that's the tab character literal

Oh yes, and boolean! It's name is bool in C++,
   and its literals are true and false

*  compound expressions: there's MORE than one way to write
   them in C++...

   There are so-called INFIX operators;

   for example: + - / *

   you put binary infix operators BETWEEN two expressions
   of appropriate types; the result is a compound expression:

   3 + 5
   7 * (8 + 6)
   
   ...use parentheses to indicate order of operations, when in
      doubt;
      C++ does have an order of precedence for all of its operators,
      if you don't;

   FOR numeric operations,
   they are different for ints and doubles,
   and the C++ compiler assumes the int operation if
       BOTH expressions have type int,
       and it assumes the double operations otherwise

    ...that is, make sure you understand why 4 / 5 is 0...

    other infix binary operations:

    relational operations, that result in a bool value:

    < <= > >= 
    !=         not equal
    ==         compare for equality

    here are the bool operators:

    &&   <--- logical and
    ||   <--- logical or
    !    <--- logical not

    true && true
    (3 < 5) || (5 < 3)
    !false

*   You can define and use functions in C++ as well;

    ...some functions are already written and ready to use,
    in C++ standard libraries;
    
    ...for example, the math standard library includes
    functions like sqrt, and ceil, and floor

    When you call a function -- that's a compound expression
    as well;

    how do you call a function?

    in Scheme:   (sqrt 2)
    in C++:      sqrt(2)

    in Scheme:   (max 2 3)
    in C++:      max(2, 3)  <-- you separate multiple expressions with commas

    Like in Scheme, the expressions that are arguments for a function
    can be ANY expression, simple or compound, of appropriate type:

    in Scheme:  (max (sqrt 2) (+ 3 5))
    in C++:     max( sqrt(2), 3 + 5.0 )  (oops -- C++'s max wants double args)

*   How about identifiers in C++?
    ...their names have fewer options than in Scheme:

    ...gotta start with a letter,
       followed by 0 or more digits, letters, or underscore

       (no -, no ? ...)

*   How about C++ comments?

Scheme:   ; a single-line comment
C++:      /* a
             multi-line
	     comment
	  */
          // a single-line comment

*   How about named constants?

    in Scheme:  (define WIDTH 300)
    in C++:     const int WIDTH = 300;
                ^ const means it CANNOT be changed (it is constant)
		      ^ int is the constant's type
                          ^ the constant's name is now WIDTH (all uppercase!!)
                                ^ assign 300 to that named constant,
				  and now after this it can never be changed
                                     ^ MUST end with a semicolon!

*   How about basic function syntax?

    in Scheme:
    (define (funct-name param-name1 .. param-namex)
        expr
    )

    in C++:
    return_type funct_name(param_type param_name1, ... param_type param_nameX)
    {
       statement;
       ...
       statement;
       return expr;  // for a function that produces a value
    }
    

    ...the value of the Scheme function is the value of the expr
       in its body;
    ...the value of the C++ function is the value of the expr
       in its return statement;   return expr;

*   We'll be compiling, running C++ on nrs-labs to start;
    ...gave out a UNIX-for-CIS-130 handout,
                  and a C++ tools handout, both also
		  available from the public course web page;

*   The C++ tools: (which we installed using the instructions in
    the appendix in the C++ tools handout...)
    *   expr_play lets you type in a C++ expression and see its value;
    *   funct_play2 lets you walk through the design recipe for a C++ 
        function;
    *   funct_compile lets you compile (and run) an already-written
        (but needing-to-be-compiled) C++ function

*   we created C++ function rect_area with the help of funct_play2;

*   we entered the following when prompted:
    *   its contract (like Scheme, except use C++ type names, and
        name the function using C++ identifier rules):

	// contract: rect_area: double double -> double

    *   its purpose:

        // purpose: it expects a rectangle's length and width,
        //          and it produces that rectangle's area

    *   its header (notice the C++ syntax for this!)

        double rect_area(double length, double width)

    *   its specific examples/tests, written as C++ bool expressions:

        rect_area(3, 4) == 12.0
        rect_area(10, 20) == 200

    *   its body (always surrounded by { and },
                  with the statement(s) INDENTED within,
		  right now a return statement whose expression
			becomes the value of this function,
	          and that return statement MUST end with a semicolon)

	{
	    return length * width;
	}
		  
*   the following files are created by funct_play2 for you,
    after you enter the above when prompted:

    rect_area.cpp (the C++ source code file,
                   with comments containing the contract, purpose,
		   and examples,
		   with the function header and the function body)

    rect_area.h (the C++ function header file,
                 with the function's header followed by a semicolon;
		 (and named constant definitions would go here, too))

    rect_area_ck_expect.cpp 
        a clunky little C++ main function that outputs the tests
        and the results of calling those tests;