CS 112 - Week 1 Lecture 2 - 2022-08-25
=====
TODAY WE WILL
=====
* more C++ review/overview
including review/overview of design recipe
and also intro to separate compilation
* going through roll...
* say something you either:
...like about C++
...wonder about C++
...are not too fond of about C++
...would like to see an example of in C++
=====
continuing our function from Tuesday!
...we have THOUGHT about the data involved,
and we have determined suitable C++ types for
handling that data.
====
next design recipe step: develop the function/method
SIGNATURE
====
* CS 112 class style:
we will write this as a comment
-----
C++ comment:
-----
* single-line: // single-line comment
* everything from the // to the end of the line
is IGNORED
* multi-line: /* everything from the slash-star
until a star-slash
is ignored */
* here's CS 112 course style for a
C++ function signature comment:
// signature: funct_name: type-of-each-arg -> type-returns
// signature: letter_match: string string int -> bool
CS 112 COURSE STYLE:
* function names should be descriptive!
and not misleading
* start with a lowercase letter
* can separate parts with _ or camelCase
====
NEXT design recipe step:
IN a comment, DESCRIBE the meaning of each argument expected,
DESCRIBE any side-effects of your function, and
DESCRIBE what it returns (if anything)
====
* CS 112 course style:
...actually say "expects ... " followed by description
of what the function expects,
...actually say "returns ..." followed by description
of what the function returns
/*
signature: letter_match: string string int -> string
purpose: expects a word-of-the-day, a word-guess,
and a desired 0-based position to check in those words,
and returns if the letter in that position
in both words match. If the position is not within
the bounds of BOTH words, this returns false.
*/
=====
design recipe next step:
write the HEADER of your function
(followed by an empty body FOR NOW)
=====
C++ function header and empty body:
ret_type funct_name(param1_type param1,
param2_type param2,
...)
{
}
bool letter_match(string word_of_day,
string word_guess,
int pos)
{
}
* CS 112 COURSE STYLE:
* use descriptive, non-misleading parameter names
* if header is long, continue on next line(s)
indenting in some tasteful fashion
* for a set of curly braces,
each is on its own line,
lined up with the beginning of the
program component before it
* and statements within the { } will be indented
by at least 3 spaces
=====
next design recipe step:
write at least 2 tests of the function being written
(and an additional test for each category of data
involved)
WHEN POSSIBLE, write these as bool expressions
=====
C++ syntax:
C++ has < > <= >=
C++ has && as well as and
|| as well as or
! as well as not
FOR EQUALITY COMPARISON,
in C++, use ==
(a single = is ASSIGNMENT)
/*
signature: letter_match: string string int -> string
purpose: expects a word-of-the-day, a word-guess,
and a desired 0-based position to check in those words,
and returns if the letter in that position
in both words match. If the position is not within
the bounds of BOTH words, this returns false.
tests:
letter_match("model", "bagel", 0) == false
letter_match("model", "bagel", 4) == true
letter_match("apple", "app", 3) == false
*/
* C++ string class happens to include the following
methods:
method at: expects position, returns the char at
that position in the calling string
method length: expects nothing, and returns the
number of characters in the calling string
C++ method call syntax:
object_expr.method_name(arg1, arg2, ...)
* a C++ statement is terminated by a SEMICOLON
* if a function or method is to return something,
you do so with a return statement:
return desired_expr;
bool letter_match(string word_of_day,
string word_guess,
int pos)
{
return (pos < word_of_day.length()) &&
(pos < word_guess.length()) &&
(word_of_day.at(pos) == word_guess.at(pos)) ;
}
* A C++ program is defined as a collection of 1 or more
functions where exactly one is named main
and that's where execution starts when it is run
(and we used the posted main template
to create a small main function to run and print
to the screen the results of letter_match's
tests -- see the posted letter_match_test.cpp)