=====
CS 111 - Week 8 Lecture 2 - 2025-10-16
=====
=====
*** 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
=====
* start reading the C++ transition readings on Canvas!
* Homework 7 will come out after tomorrow's labs
* watch for a class e-mail letting you know when
it is available
=====
* there are new handouts/reading on the course Canvas site,
under Modules, in section "C++ Transition Readings"
* 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)
* so -- let's discuss that difference a bit:
=====
Compilers and Interpreters
=====
* BOTH of these are kinds of 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 BSL 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 |
|--------| | | |-------------|
|----------|
* your source code file(s) are the INPUT to the compiler program!
* the compiler OUTPUTS a (linked-and-loaded) executable file
* 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.
=====
concept of a PROGRAM in C++
=====
* 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*!)
* but -- in Racket, we can define a program informally as any collection of
functions and definitions that we want to work together to do something;
* C++ has a more specific definition of a program:
one or more functions, one of which has the special name main
(plus any additional desired definitions, such as named constant
definitions)
and when you execute a compiled C++ program,
it starts by running that main function
* it still has simple expressions,
and compound expressions, and functions, BUT with the above
structure
* also: while in BSL Racket, we typed in expressions, and could
then run individual expressions,
C++ puts expressions into STATEMENTS
(roughly: a statement is like a sentence)
... a thing to be done;
=======
starting C++ syntax (and semantics)
=======
=======
comments!
=======
* single-line comment:
Racket
; I am a comment
C++
// I am a comment
* C++ multi-line comments are also very popular:
/* ignore EVERYTHING
even newline characters
until you get to */
======
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;
======
some C++ data types
======
* boolean-style data type:
=====
Racket: data type boolean #true #false
C++: data type bool true false
=====
* numeric data types:
=====
Racket: data type number 1 34 3.4 -0.555
* (OK, under the hood Racket actually has several numeric data
types, but we did not have to get into that yet...)
C++ - we have to admit from the start that there is more
than one numeric data type
data type int - integer type - 1 -3 245 +34
* when you type a literal simple expression of
digits (optionally with + or - in front),
C++ considers that to be type int
data type double - double-precision floating point (~16 significant digits)
* when you type a literal simple expression of digits
(optionally with + or - in front)
that INCLUDES a decimal point, C++ considers that to be type
double
* CS 111 class style: yes, there is also float,
but we won't be using that
(likewise for long, short)
=====
* what about strings?
=====
Racket: data type string "moo"
C++ has TWO stringy types
* the oldest, original stringy data type is:
char*
* in C++, when you type anything in double-quotes,
it is considered to be a simple expression literal
of data type char*
* the newer, better, more versatile data type in C++
is provided by a class, and its name is:
string
* BUT -- you cannot type a simple expression literal
of data type string ...!!!
(we couldn't type a simple expression literal
for Racket's scene data type, either...)
* HOWEVER -- YOU CAN ASSIGN char* LITERALS TO C++ parameter variables
and other variables whose data type is string!!!!!!!
* And C++ string expressions are more versatile,
less error-prone,
and have more built-in operations and functions available
than C++ char* expressions do!
* SO: CS 111 class style:
* for stringy data, (declaring function return types
and parameter variables and named constants
and local variables), you are expected to use
type string, NOT char* !!!
* (and rest assured that you CAN assign char* simple
expression literals to those string parameters, etc.)
=====
* oh and there's a data type for a SINGLE character...
=====
* ...we ignored that in Racket,
* ...in C++, it is the data type char <--- NOT char*
* anything representing a single character typed in SINGLE
quotes is considered to be of type char
'a' - char
'\n' - char (a newline character)
=====
SO: as in Racket, in C++ the SYNTAX you use for a simple expression literal
DETERMINES its data type!
=====
* for example:
"a" // data type char*
7 // data type int
7.0 // data type double
'7' // data type char
"7" // data type char*
true // data type bool
"true" // data type char*
=====
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 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;
* and, we'll discuss C++ arrays later in the semester
=====
C++ compound expressions
=====
* BSL Racket has ONE compound expression syntax:
(op arg arg arg ...)
* C++ has arguably -- several...
====
* C++ compound expressions involving infix arguments
====
* infix? BETWEEN its operands
+ - * /
< > <= >=
and for comparing for equality: == <-- not to be confused with assignment =
&& and <-- boolean and operation
|| or <-- boolean or operation
3 + 5
3+5
4 * 6
4+6*3
=====
* another compound expression type: put any expression in parentheses
=====
( expr )
(expr)
* that is, you can surround any expression with a set of
parentheses
* evaluates the innermost parentheses first
(4 + 6) * 3 // will have the value 30 (10 * 3)
4 + (6 * 3) // will have the value 22 (4 + 18)
100 - (6 * (2 + 7)) // will have the value 46 ( 100 - (6 * 9))
// 100 - 54 )
((((((((3)))))))) // please don't -- but has the value 3
* what if you have several infix operators and NO parentheses?
* get C++'s variant of PEMDAS -- operator precedence!
* the precedence of the operator determines which operator's action
is done FIRST -- the operator with highest precedence is done first
* (I have included a link to a chart of C++ operator precedence
with these notes!)
* * does have higher precedence that +,
so, for example, 3 + 5 * 6 will have value 33 (3 + 30)
* GOOD STYLE: use parentheses to make the order of operations
CLEAR to you AND to all future readers!
=====
* C++ has some prefix operations
=====
prefix-op operand
! not <--- boolean negation
!true
!false
not true
not false
=====
* C++ also has some postfix operations, but we will not get into those today.....
=====
=====
* then there is the syntax for C++ function calls, another kind of
C++ compound expression!
=====
in Racket: (max 3 5)
in C++: max(3,5) max(3, 5)
* in C++, MUST have an opening parenthesis AFTER the function's name
in C++, if a function call has more than one argument expression,
MUST *separate* them with a COMMA
=====
* named constants:
=====
Racket:
(define FIXED-COST 210.5)
C++:
const double FIXED_COST = 210.5;
=====
C++ statements
=====
* typically ends with a semicolon
* You will see some of these in tomorrow's lab exercise.
* (there is another special kind of statement called a BLOCK --
more on those soon!)
=====
* You'll see an example C++ program in lab tomorrow,
and be introduced to the programming environment we will be using for C++.
And we'll talk more about C++ functions and more next week!