=====
CS 111 - Week 8 Lecture 2 - 2026-10-15
=====
=====
*** NOTE: cleaned up these notes and added to them
after class --
LET ME KNOW if you have any questions about them!
=====
=====
TODAY WE WILL:
=====
* announcements
* start intro to C++!
* prep for next class
=====
* CLASS "QUILT" POSTED!
* see public course web page, Canvas home page!
=====
* 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
=====
* 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
======
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)
* we'll say more about interpreters and compilers later;
* but note that, for a given programming language,
it may have BOTH interpreters and compilers available
for it.
that is,
* 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++.
=======
starting C++ syntax (and semantics)
=======
=====
Comments!
=====
* ; I am a Racket single-line comment!
* // I am a C++ single-line comment!
* #| Racket
multi-line
comment |#
* /* C++
multi-line
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;
=====
* 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
* C++ data type int - integer type
* C++ int syntax:
set of digits, NO decimal point
optional + or - at the beginning
* C++ considers each of these to be of type int:
7
3
132
-11
0
* C++ data type double - double-precision floating point
* ~16 significant digits
* C++ double syntax:
set of digits WITH a single decimal point
optional + or - at beginning
* C++ considers each of these to be of type double:
7.0
0.0
.333333
-8.300003
* CS 111 class style: yes, there is also a type float,
but we won't be using that; you are expected to use
double for non-integer numeric values
* and you are expected to use int for numeric values
that are expected to be integers
* (likewise, we will not be using types long, short
in CS 111)
=====
* Boolean data types:
=====
* Racket - data type boolean - #true #false
* C++ - data type bool:
true
false
(sadly, C++ will also treat a 1 in a boolean context as
true, and a 0 in a boolwan context as false)
* CS 111 class style standard: use true and false for
C++ bool expressions instead of 1 and 0
=====
* string-like data types
=====
* Racket - data type string - "moo" "baa" "la la la"
* C++ has TWO string-like types!
* char* - sometimes called old-style C-strings; often pronounced "care-star"
"mooo" - anything in double quotes is treated as type char*
* string (the string class) - it was added later
* has more capabilies than a char* value
* you cannot write a literal of type string,
BUT you can assign char* literals TO a string
named constant, parameter, etc., and it
WILL be converted appropriately!
* 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 did not happen to get to this type character in Racket;
#\a #\newline
* BUT, for a reason we will discuss later, it will be USEFUL
to get to this type in C++ --
its type name is:
char
* (do not confuse char and char* --
char is a SINGLE character,
char* is a SET of character(s))
* syntax: put single quotes around something
that is to be considered a SINGLE character
'a' '\n'
=====
SO: as in Racket, in C++ the SYNTAX you use for a simple expression literal
DETERMINES its data type!
=====
* for example, in C++:
"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
=====
compound expressions
=====
* BSL Racket: has just ONE compound expression syntax:
(operation arg1 ... argN)
* C++ has several kinds of compound expressions...
=====
* using INFIX operators (IN-between its arguments/operands)
=====
+ - * / <-- arithmetic infix operators
< > <= >= <-- some relational operators
for comparing for equality: == <-- not to be confused with assignment = !!
&& and <-- boolean and operation
|| or <-- boolean or operation
3 * 4 // 12
7.0 - 1 // 6.0
4 + 5 // 9
3.0 / 4 // 0.75
3 / 4 // 0
4 / 3 // 1
3 % 4 // remainder from integer division, 3
some logical infix operators - these all return bool values
3 < 4
3 > 4
3 <= 4
3 >= 4
3 == 4 // NOTE the == is used for equality comparison!!!!!
// = is used for ASSIGNMENT, for giving something a VALUE
=====
* 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
=====
* compound expression syntax for them:
prefix-op operand
* ! not <--- boolean negation
!true // has the value false
!false // has the value true
not true // has the value false
not false // has the value true
=====
* C++ also has some postfix operators, 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: (pow 3 5)
in C++: pow(3,5) pow(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:
=====
* in Racket:
(define MAX 100)
* in C++:
* an assignment operator, =, is used to assign a value to a C++
named constant,
in a declaration *statement*:
const double MAX = 100;
* MUST include const, to say this cannot be changed
* MUST include the data type of the new named constant
* CS 111 class style: C++ named constants are still written in all-uppercase
* then an assignment statement, =
* then an expression whose value will be the value of this named constant,
* and this must be ENDED with a semicolon ;
=====
C++ statements
=====
* typically ends with a semicolon
* You will see some more of these in tomorrow's lab exercise.
* (there is another special kind of statement called a BLOCK --
more on those soon!)
=====
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;
* we'll talk more about these, also.
=====
* 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!