=====
CS 111 - Week 8 Lecture 2 - 2024-10-17
=====

=====
*** NOTE: added notes after class summarizing
    what we discussed about C++ compiling,
    and also some more C++ compound expression details,
    and other bits here and there --

    READ through, and LET ME KNOW if you have any questions!
=====

=====
TODAY WE WILL:
=====
*   announcements!
*   start intro to C++!
*   prep for next class

=====
*   Homework 7 will come out after tomorrow's labs --
    watch for a class e-mail when it is available!

=====
*   there are new handouts/reading on the course Canvas site:
    *   C++ transition readings
    *   a handout: "Some Racket/C++ Comparisons"
    *   a C++ version of the "graphic design recipe" handout

======
INTRO to C++!
======
*   ...as a SECOND programming language!

*   (OK, and also introducing some concepts we hadn't gotten to yet
    in Racket in C++, in a few weeks)

=====
*   Why C++?
    *   it is very widely-used;
    *   it is fast in execution;
    *   many recent languages "borrow" syntax from the C/C++ family
        of languages;

=====
*   some differences between Racket and C++:
    *   C++ is "closer to the machine"

    *   C++ is strongly-typed --
        C++ function headers WILL require type information to be
	included, which Racket function headers did not;

    *   Racket is typically interpreted (to translate it to
        computer-hardware-level instructions)

	C++ is typically compiled (to translate it to
	computer-hardware-level instructions)

=====
DEFINITION of a C++ program
=====
*   because in C++, one generally can't just say,
    run a function blah

*   C++ DOES still have simple and compound expressions!
    *   still has functions, too!

*   BUT C++ puts expressions into STATEMENTS
    (roughly: a statement is like a sentence)
    ...a thing to be done;

*   in DrRacket, we could define and run any function we wanted!

    We could informally consider any collection of related functions
    and definitions to be a "program".

    *   in C++, there is a more formal definition of a program:

        ****************
        A C++ program is a collection of 1 or more function definitions
        (and 0 or more other definitions),
        ONE of which is a function with the special name: main
        ****************

    *   And main can have one of 4 possible function headers --
        we will be using this one:

        int main()

    *   And when you run a C++ executable file that you have
        compiled,
	it STARTS by running this main function

        *   that is -- MOST C++ environments do not give you a way
	    to run an individual non-main C++ function by itself;
	    to run such a function, you have to call it from a main
	    function (or from another function called from a main
	    function, etc.)

=====
*   and if you want to see the value of something,
    you'll generally need to ask that it be printed out --
    we'll be getting to that shortly!

    *** BIG THING: printing is NOT the same as returning! ***

=====
BUT!!!!
*   a C++ program is, like a Racket program, just a finite, ordered
    list of instructions you would like the computer to do for you!

    we still have syntax! (language rules!)
    we still have semantics! (what expressions and statements *mean*!)

=====
Compilers and Interpreters
=====
*   BOTH programs that are used to translate high-level programming
    language expressions and statements into lower-level instructions
    the computer can execute

*   an interpreter (DrRacket provides (mostly) an interpreted environment)
    translates AND then executes each expression EACH time you ask
    the interpreter to run

    a compiler, on the other hand,
    translates and then saves the lower-level instructions into a
    separate file,
    which can be linked and loaded into an executable file;
    then, the user runs that executable whenever and as many times
    as they would like (without having to RE-translate each time)

=====
*   NOTE:
    for a given programming language,
    it may have BOTH interpreters and compilers available for it!

    *   there ARE Racket compilers out there!
    *   I've heard there are C++ interpreters out there!

    ...but we happened to use an interpreted-style environment for Racket,
    ...and we will be happening to use an environment for C++
       that includes a compiler for C++.

*   so, when using a compiler:

    ---------|     |----------|   |-------------|
    | source |     |          |   | executable  |
    |  code  |---->| compiler |-->|    code     |
    | file(s)|     | program  |   |   file(s)   |
    |--------|     |          |   |-------------|
                   |----------|

    *   your source code file(s) are the INPUT to the compiler program!
    *   the compiler OUTPUTS (linked-and-loaded) executable file(s)

    *   if you have a syntax error in your source code,
        the compiler complains, and does NOT output any executable file!!!

    *   IN C++, the source code file(s) typically end in .cpp
    *   IN C++, the executable file typically ends with NO suffix

=====
*   a compiled executable file will usually only run on
    the operating system and computer hardware it was designed to
    run on --

    to run a C++ source code program on different operating systems
    and on different computers, you will tend to need to compile it
    on the desired computer

    *   but we'll be using a browser-based environment for our C++,
        which should (fingers crossed!) keep things simpler!

=====
*   so, typical work flow in CS 111's C++ portion:
    *   using the design recipe, you design your C++ functions
        in a C++ source code file whose name ends in .cpp
        *   and include a main function in that file

        *   (this is not the ONLY option, but it is a good way
	    to start!)

    *   you COMPILE this .cpp file
        and if your syntax is correct, you get an executable file
	as a result
	*   if your syntax is not correct, you try to correct it
	    and then you compile it again -- repeat until you get
	    an executable file!

    *   you then run that executable file to run your program
        and see if it worked!
	*   and debug your source code and recompile as needed.

=====
C++ SYNTAX - COMMENTS
====

Racket:

; I am a single-line Racket comment!

C++:

// I am a single-line C++ comment!


/* I am a multi-
   line
   C++ comment */

======
C++ Simple expressions and basic data types
=====
*   C++ still has simple expressions,
    each of which is considered to have a data type,
    and you can have literal simple expressions of
        at least some of those data types

*   but C++ has some different data types,
    and some of the similar ones have different names;

=====
Racket number - in C++?
=====
*   Racket actually has multiple numeric types --
    BUT it is not strongly-typed, so I could just call
    them all number

*   C++ is strongly typed, so have to know more its
    numeric type names

    *   there are at least 6 numeric types in C++,
        in CS 111 we only care about these two:

	int - for integer
	double - for double-precision floating point

        if you type digits with no fractional place,
	(optionally starting with + or -),
	C++ says: that's type int

	8 24 -555 +34         // type int

        if you put a decimal point in, C++ says: that's type double

	8.0 -3.33 +5.666667   // type double
	
=====
Racket list - in C++?
=====
*   in Racket, the most-basic data structure is
    (arguably) the list

*   in C++, the most-basic data structure is
    (arguably) the (1-dimensional) array

    *   we'll see there are some important differences between
        these, BUT both are a set of things with a single name;
	
=====
Racket boolean - in C++?
=====
*   Racket: #true #false

*   C++ has a type:  bool
    and its literals:   true false

    *   although (sigh) it sometimes treats 1 as true, and 0 as false

    *** CS 111 course style: use true and false only for type bool ***

=====
Racket scene - in C++?
=====
*   C++ does not have a primitive type for this
    (but you could write a class to implement this type,
    after CS 112, anyway 8-) )

=====
Racket image - in C++?
=====
*   C++ does not have a primitive type for this
    (but you could write a class to implement this type,
    after CS 112, anyway 8-) )

=====
Racket string - in C++?
====
*   Racket: "moo"

*   C++ has two stringy types you need to be aware of
    *   IF you type a thing in double-quotes:

    	"moo" // type is char*, a so-called "old style" C-string

    *   BUT there is now a string class as part of one
        of C++'s standard libraries <-- like our Racket modules

        instead of require:	    (require 2htdp/image)

	you #include:		    #include <string>
	                            using namespace std;

        ...and now you have a type string
	   that char* literals CAN be assigned to -
	   (a char* literal CAN be an argument for a string parameter)

        *   but there isn't a way to write a literal of type string
	    (like, in Racket, we didn't have a way to write a literal
	    of type scene, either...)

        *   for now: NOTE this C++ class style standard:
	    when declaring a function or a named constant,
	    USE string NOT char*

	    *   and then, even though "anything in double quotes"
	        is a literal of type char*, it CAN be the argument
		for a string parameter

=====
also: C++ char type
=====
*   (Racket has a character type, we just didn't get to it --
    you can see it on the "Some Racket-C++ Comparisons" handout)

*   IF you write something
    representing a SINGLE CHARACTER in single quotes,

    that's type char in C++

    'a' - char
    '\n' - char, a newline character

=====
*   NOTE then -- just like in Racket, the SYNTAX you use
    to write a C++ simple literal expression determines its data type!
    
    ...using C++ data types and syntax instead of Racket ones, of course;

*   SO, for example:

    1   // int
    1.0 // double
    "1" // char*
    '1' // char
			
=====
C++ compound expressions
=====
*   BSL Racket had ONE compound expression syntax:

    (operation arg1 arg2 ... )

*   C++ has SEVERAL varieties of compound expression syntax,
    depending on the operation involved!

*   C++ DOES have function calls:

    Racket: (my-fun arg1 arg2 ..)

    C++:    my_fun(arg1, arg2, ...)

*   IF you have a C++ INFIX operator,
    it goes IN-beteen its arguments:

    3 + 5
    3 * 5
    3 - 5
    3 / 5
    3 % 5 // modulo!

    true or false   // TWO ways to write logical or
    true || false

    true and false  // TWO ways to write logical and
    true && false

    !true           // TWO ways to write logical not
    not true
    *   oh, wait -- these are PREFIX operators!
        You put ! or not BEFORE their single argument, assumed to be
	of type bool!

    3 < 5
    3 <= 5
    3 >= 5
    3 > 5
    3 != 5  // not equal

    *********************************
    AND to compare if EQUAL:	==
    3 == 5
    *********************************
    (= is the ASSIGNMENT operator in C++)

    *   a SUPER-COMMON C++ ERROR: using = when you mean == !!!!!!!!!

    =====
    *   the type of an infix operator may depend on the
        type of the arguments

        3 + 5     // when an arithmetic infix operator has two
	          //    int arguments, it does the int version of
		  //    that operation -- here, int addition, resulting
		  //    in int value 8

        3.0 + 5.0 // when an arithmetic infix operator has two
	          //    double arguments, it does the double version of
		  //    that operation -- here, double addition, resulting
		  //    in double value 8.0 <-- ALTHOUGH this will be
		  //                            PRINTED as 8 usually!!

        3.0 + 5  // when an arithmetic infix operator has a double AND
              	 //    an int argument (or int and double),
		 //    it does the double version of
                 //    that operation -- here, double addition, resulting
                 //    in double value 8.0 <--	ALTHOUGH this will be
                 //  	      	      	      	PRINTED	as 8 usually!!
		 
	"moo" + my_string_param
                // here, because + has arguments char* and string,
		//     it will use the string + operator and APPEND these
		//     to result in a string with the appended result
		// (+ works as append if at least one of its arguments
		//     is type string -- the other can be string, char*,
		//     or even char...!)

        "Moo" + "oooo"
                // here, because + has two char* arguments, you'll
		//     get an ERROR, because char* does not have a +
		//     operator

        // and WATCH OUT for cases like THIS:

	3 / 5  // int division! ya get 0!!!!!
	       //     That is, because + has two int arguments here,
	       //     you get integer division, where any remainder is
	       //     ignored (any fractional part essentially
	       //     truncated) -- and int division of 3 by 5 is thus 0!

*   C++ also has POSTFIX operators, but we'll
    give an example of the most-classic of these later...

=====
C++ and PARENTHESES
=====
*   ALSO NOTE:
    NOW you can use parentheses more similarly to the way you do in Math!

    Another compound expression in C++ is, you can put parentheses around
    ANY expression!

    3
    (3)
    ((3))
    (3 + 5)
    (3 + 5) * 6

    etc.!

    *   and like in Math's PEMDAS, parentheses let you specify to do one
        operation before another --

	(3 + 5) * 6   // that is 8 * 6 or 48
	3 + (5 * 6)   // that is 3 + 30 or 33

    *   hey, what is 3 + 5 * 6  (if no parentheses?)

        C++ decides based on operator precedence!
	(I'll link a chart of C++ operator precedence with these notes!)

        * does have higher precedence than +,
	so 3 + 5 * 6 will have the value 33,

	BUT when in doubt, use parentheses to make this clear to
	you AND all future readers!!!

=====
C++ statements
=====
*   typically ends with a semicolon

    *   You will see some of these in tomorrow's lab exercise.

*   but this is also a statement:

    {
    }

    *   this is a BLOCK, and can contain 0 or more statements...!
        More on this later, but you will SEE some blocks in tomorrow's
	lab exercise.

=====
C++ identifiers
=====
*   whether in Racket or in C++,
    an identifier is a name chosen by the programmer,
    and includes function names, named constants, parameters,
    and more;

*   in Racket, (adapting from Week 2 Lecture 2 projected notes):

    *   In BSL Racket, syntax for an identifier
        is any collection of characters that
        does NOT include a blank or tab or newline
        AND does NOT include any of the special characters:

        ( ) [ ] { } " , ' ` # | \

        AND ALSO does not follow the syntax rules for
        a Racket type

        (also: it can't have already been defined in the
        current scope! 8-) )

*   In C++: the identifier syntax is more restricted, and so simpler:

    *   In C++, an indentifier starts with a letter
        followed by 0 or more letters, digits, or _ 

        AND ALSO doesn't follow the syntax rules for a C++ type

        AND ALSO it can't have already been defined in the
	current scope;

    *   CS 111 course style: still want them to be descriptive,
        not too generic, and not misleading!

    *   CS 111 course style: named constants in all-uppercase,
    
        function and parameter names in all-lowercase
	(but mixedCase, starting with a lowercase letter, accepted also
	for function and parameter names)

=====
*   You'll see an example C++ program in lab tomorrow,
    and we'll talk more about C++ functions and more next week!