=====
CS 111 - Week 10 Lecture 1 - 2025-10-28
=====
=====
TODAY WE WILL:
=====
* announcements
* concept of a class, and calling a class method
* intro our first C++ branching statement: if statement
* prep for next class
=====
* should be working on Homework 8!
* at-least-first-attempts due by 11:59 pm on Friday, October 31!
* MEET WITH YOUR ADVISOR(S)!
* RE-CREATE your DARS plan!
* get ready to REGISTER for SPRING 2026 between November 10-21!
* CS and SE majors:
* in Spring 2026, you should take:
CS 112 - CS Foundations 2
Math 253 - Discrete Math
* You CAN take Math 101T - Trigonometry and Math 253 CONCURRENTLY
* You CAN ask to switch to another ENGL 103 section if it conflicts
with a major requirement such as CS 112 or Math 253 that you need to
stay on-track in your major!
=====
consider our collection of C++ compound expression
syntaxes so far:
=====
expr op expr
( expr )
op expr
expr op
my_funct(expr, expr, ... expr)
...now we'll add another!
=====
FIRST: we need to define the term CLASS
=====
...in one of the categories of OBJECT-ORIENTED computer languages,
a CLASS is how a programmer can define a customized data type
* a class gives you a way to say, I want to gather
data together as one type, and here also are the
OPERATIONS for OPERATING on that type of data;
* a C++ class allows you to define (overload) operators
for class instances, as well as other function-like
operations for class instances;
* one common term for these function-like operators
a class defines are METHODS (another is member functions)
* another common term: is when you declare an instance of
a class, that's also called an OBJECT
* in CS 112, you will write your OWN C++ classes --
in CS 111, you will USE some classes defined by C++
standard libraries;
for example: the string type is a class defined
by the C++ string library!
* so: it turns out that the string class has some lovely
methods, and we'd like to use them!
=====
* so: here's the syntax for calling a method of
an object (of an instance of a class)
=====
desired_obj.method_name(expr, expr, expr, ...)
=====
FOR EXAMPLE: the string class has a length method!
=====
* the length method expects nothing (!!), and returns
the number of characters in the calling string object
(the calling string instance)
const string FIRST_COURSE = "CS 111";
FIRST_COURSE.length() // a compound expr whose value is 6
and method at expects a desired position in a string,
and returns the char at that position (BUT the position
of the first character in a string is position 0...!)
FIRST_COURSE.at(0) == 'C'
FIRST_COURSE.at(1) == 'S'
=====NOTE!!!=====
* cout uses a default format based on the type of expression
it is displaying to standard out!
for a double? only displays about 7 significant digits, max!
AND if its fractional part is .0 -- for example, 4.0 --
cout displays it WITHOUT a decimal point...!
cout << 1.234567890 << endl; // displays 1.23457
cout << 4.0 << endl; // displays 4
for a char* or string? NO double-quotes are displayed
for a char? NO single-quotes are displayed
=====
C++ branch statement NUMBER 1: the if statement
=====
* the SIMPLEST if statement:
=====
* SYNTAX:
if (bool_expr)
statement;
BECAUSE you can ONLY have a single statement after the if,
and BECAUSE we often want to do more than one thing in a situation,
and BECAUSE a BLOCK is considered a single statemendt by the C++
compiler,
I tend to always use:
if (bool_expr)
{
statement;
}
...so I don't forget if I later change it to:
if (bool_expr)
{
statement;
...
statement;
}
* SEMANTICS of this:
if bool_expr is true, DO the statement following,
otherwise just continue without doing it.
* (I drew a flow-chart of this on the board, and after
class made a little handout including it --
see simplest-if.pdf, posted with these class notes
and with the C++ Transition Readings on Canvas)
=====
* OPTIONALLY, you can have an else part with the statement
to be done if bool_expr is false
=====
* SYNTAX:
if (bool_expr)
statement1;
else
statement2;
and because, again, there can only be ONE statement after the else,
I tend to just always to always use a block after the else:
if (bool_expr)
{
statement1_1;
...
statement1_n;
}
else
{
statement2_1;
...
statement2_n;
}
* SEMANTICS of this:
if bool_expr is true, DO the statement1 following the if part,
otherwise DO the statement2 following the else part
* NOTE: you will NEVER do both! You will do
EITHER the if's statement OR the else's statement!!
* (I drew a flow-chart of this on the board, and after
class made a little handout including it --
see if-else.pdf, posted with these class notes
and with the C++ Transition Readings on Canvas)
=====
Can you have an if statement as part of an if statement?
As the if's statement? As the else's statement? In a block?
=====
* Sure! When we say "statement", that means any statement,
including another if statement!
(but every else has to be part of an if...!)
* SO: so many combinations are possible!
* And you want to choose one that works for what YOU want
to do!
* for example, if you have multiple choices and may choose 0 or more
of them,
just use a sequence of if statements!
if (bool_expr1)
{
statement1;
}
if (bool_expr2)
{
statement2;
}
if (bool_expr3)
{
statement3;
}
* do you see that, in theory, depending on the bool_expr expressions
involved, you might do 0 or more of statement1, statement2, statement3?
=====
if-else-if pattern - when you are making EXACTLY 1 choice from 3 or more options!
=====
* if you want, though, to make exactly ONE from 3 or more
choices, here is a common accepted alternate indentation
for a chain of ifs --
* (I call this the if-else-if pattern)
if (bool_expr1)
{
statement1;
...
}
else if (bool_expr2)
{
statement2;
...
}
else if (bool_expr3)
{
statement3;
...
}
...
else
{
statement_for_else;
...
}
* SEMANTICS: the above is ANALOGOUS to BSL Racket's cond expression!
and behaves similarly!
...each bool_expr is attempted until one is true,
and that bool_expr's action/statement is done
and the rest are SKIPPED!
(cond
[bool_expr1 result_expr1]
[bool_expr2 result_expr2]
[bool_expr3 result_expr3]
...
[else result_for_else]
)
* (I drew a flow-chart of this on the board, and after
class made a little handout including it --
see if-else-if-pattern.pdf, posted with these class notes
and with the C++ Transition Readings on Canvas)
* and this approved alternate indentation is easier to type and read than:
if (bool_expr1)
{
statement1;
...
}
else
{
if (bool_expr2)
{
statement2;
...
}
else
{
if (bool_expr3)
{
statement3;
...
}
else
{
statement_for_else;
...
}
}
}