=====
CS 111 - Week 9 Lecture 2 - 2026-10-22
=====
=====
TODAY WE WILL:
=====
* announcements
* a point about C++ relational operators
* a few useful CS50 tidbits
* a function involving strings
* how to call a C++ method
* a function that calls a C++ string method
* prep for next class
=====
FUN FACT: the string class defines + as an operator
for its instances!
=====
* if + has two string expressions as its operands,
OR one string expression and one char* expression,
these will be appended into a new string instance
(that is, this is C++'s version of Racket's
string-append)
* for example:
const string FIRST = "Charlie";
const string LAST = "Brown";
cout << (FIRST + LAST) << endl; // CharlieBrown
BUT!!!!! this only works if at least ONE of + operands
is a string instance!
the other CAN be a char* or even (gasp!) a char...!
cout << (FIRST + " " + LAST) << endl; // Charlie Brown
* (note: first computes FIRST + " ", a string and a char*,
resulting in a string -- then computes the result
of appending THAT string to the string LAST)
cout << (FIRST + '-' + LAST) << endl; // Charlie-Brown
* (note: first computes FIRST + '-', a string and a char,
resulting in a string -- then computes the result
of appending THAT string to the string LAST)
BUT this FAILS:
cout << ("m" + "oo") << endl; // fails, both are char*
* after class, added these to 111lect09-2.cpp, to the end
of its main function, so you can see them in action
=====
Object-oriented programming and classes
=====
* in C++, you can define a class to make
a new data type
* The string class is defined in the string
standard library to provide C++'s "modern"
stringy type
* An instance of a class is called an object
(thus, the term object-oriented programming)
* when you define a named constant or a parameter
to be of type string, they are considered to be
string objects
* A class can define data fields for data of
each of instances, and methods for operations
on its instances
* 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)
* 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)
=====
* (yet another kind of C++ compound expression!)
object_expr.method_name(arg_expr, arg_expr, ...)
* for example:
const string FIRST_COURSE = "CS 111";
* So, named constant FIRST_COURSE is a string
object; it is an instance of the string class
* C++'s string class has a length method!
The length method expects nothing, and
returns the number of characters in the
calling string:
FIRST_COURSE.length() // so, this has the value 6