=====
CS 111 - Week 9 Lecture 1 - 2025-10-21
=====
=====
TODAY WE WILL:
=====
* announcements
* continue intro to C++!
* identifiers
* designing C++ (non-main) functions
using the design recipe (and testing them)
* prep for next class
=====
* should be working on Homework 7!
=====
* New Degree Audit Report for Students (DARS) Plans Kick-Off Party
* WHEN: Wednesday, October 22, 2025, from 12:00 - 3:00 pm
* WHERE: in the Kate Buchanan Room, on the second floor of the
Gutswurrak Student Activities Center, Room 225
* Academic Advisors will demonstrate and guide students
through the new DARS planning tools and help students create
semester plans in preparation for spring ‘26 class registration.
* Pizza, t-shirts, and stickers will be provided while supplies last.
* Participants are encouraged to bring their own device.
* Please help spread the word!
=====
* please DO download copies -- of your .cpp files especially --
from the CS50 IDE regularly!
=====
* difference between char* and string
* both are C++ types -- for "stringy" data
* char* is the original, first C/C++ "stringy" type
* when you type a simple expression literal with
double quotes, the C++ compiler considers that
to be of type char*
* string is the more-modern C++ "stringly" type,
provided by the C++ string library
* it has operators and special functions called
methods that char* does NOT have
* string is a type provided by a class
* you CAN assign a char* literal to a string parameter,
you CAN use a char* literal in a return from a function
intended to return a string
* there is not a way to type a literal of type string
* CS 111 CLASS STYLE: when writing function headers, declaring
parameters, declaring named constants, and (eventually) declaring
local variables, if they involve "stringy" data, you area expected
to use string instead of char* in those.
=====
C++ identifier
=====
* more restricted than Racket's identifier syntax
* must start with a letter (a-z, A-Z)
and can be followed by 0 or more letters, digits, or underscores (_)
* (still no blanks! 8-) )
* cannot be an already-defined name
* same style rules as Racket!
* choose them to be meaningful! descriptive!
not too generic! not misleading!
* named constants should be written in ALL-UPPERCASE
* parameters and function names (and local variable names)
should start with a lowercase letter
* I will accept (as CS 111 class style) either
next_item or nextItem
1st_prize - no! can't start with a digit
prize2 - sure!
third-prize - no! can't contain a -
fourth prize - no blanks in a name!
string - that's an already-defined name!
a_string - that's fine
aString - also fine
=====
and named constant reminder:
=====
Racket: (define FIXED-COST 210.50)
C++: const double FIXED_COST = 210.50;
* and in both, once defined, that name is now
a simple expression:
FIXED_COST // a simple expression of type double
=====
a few more comments about C++ compound expressions
=====
* say a have a function with parameters quant and unit_cost,
and in the function body I want to compute their product?
quant * unit_cost
* C++ has a concept of overloading
an operator or function can have more than one meaning
depending on its context
* if * is between two int values, you get int multiplication!
if * is between two double values, you get double multiplication!
if * is between an int and a double, the C++ compiler
quietly adds what's needed to convert the int to a double
and do double multiplication
* and this is true likewise for +, -, /
* so NOTE: if you have / with two int operands,
YOU GET int DIVISION! with an int RESULT!
(3 / 4) == 0
(3.0 / 4) == 0.75
(3 / 4.0) == 0.75
(3.0 / 4.0) == 0.75
* and there is a modulo operator: %
and like Racket's modulo function, it returns
the remainder after int division
(3 % 4) == 3
(5 % 4) == 1 etc.
======
* then we walked through designing a C++ function circ_area
using the design recipe --
see 111lect09-1.cpp posted with these notes