CS 111 - Week 8 Lecture 2 - 2016-10-13
* 3 + 5 <-- + is an infix operator
(for infix operators, the operator goes between
its two operand expressions)
++value <-- ++ is a prefix operator to be described later
(for prefix operators, the operator goes before
its single operand expression)
value++ <-- ++ is ALSO a postfix operator to be
described later
(for postfix operators, the operator goes after
its single operand expression)
AND.. there's also a different syntax for calling
functions, but we'll get to that later today
* C++ does have relational and boolean operators,
also:
< > <= >=
!= <-- not equal
== <-- equal - THIS is how you compare two
values for equality in C++!
(well, int and double and char
and bool and string...
NOT char*, sadly...)
&& and <-- these are both ways to
"spell" the C++ boolean and operator
|| or <-- these are both ways to
"spell" the C++ boolean or operator
! not <-- these are both ways to "spell"
the C++ boolean not operator
* what about functions?
how do you call those?
Racket -> (sqrt 4)
C++ -> sqrt(4) ...move the open parenthesis
after the function name
Racket -> (max 3 5)
C++ -> max(3, 5) ...separate argument
expressions with commas
(white space is optional)
* C++ still has identifiers --
names the programmer chooses --
BUT they have stricter syntax than Racket:
* must start with a letter (A-Z and a-z are letters
for example)
* then followed by 0 or more
letters, digits, or underscores _
^ 0-9
* (no spaces, no dashes, no ?, no periods,
no other special characters)
* C++ still has named constants,
and here is their syntax:
(and it is STILL class style to write named
constants in all-uppercase...)
Racket: (define FIXED-COST 210.50)
C++: const double FIXED_COST = 210.50;
const desired_type DESIRED_CONST_NAME = expr;
^ this is a STATEMENT, and it must
be ended in C++ with a SEMICOLON
(EXPRESSIONS in C++ don't have semicolons!!!!!!!
...they are kind of like a word in a sentence;
STATEMENTS in C++ often DO end with a semicolon!!!!!
...they are kind of like a sentence, and need
ending "punctuation"...!)
* in C++, MOST declarations
require you to specify the
type as well as the name
of the thing being defined!
^ that's part of why it is
called "strongly typed"
* C++ comments:
// this is a single-line comment
/* this is
a multi-line
comment */
* let's write our first (non-main) C++ function
* we'll use funct_play
and funct_compile
for these until we cover main functions
* remember the syntax for a Racket function?
in RACKET we would say:
(define PI 3.14159)
(define (circ-area radius)
(* PI radius radius)
)
(circ-area 10)
* here's the syntax for a C++ function:
const double PI = 3.14159;
double circ_area(double radius)
{
return PI * (radius * radius);
}
circ_area(10)
* here is the syntax for a C++ function,
in general:
ret_type funct_name(param1_type param1,
param2_type param2,
...
paramN_type paramN)
{
stmt;
stmt;
...
stmt;
return expr;
}
NOTE that the function header, here, is
ret_type funct_name(param1_type param1,
param2_type param2,
...
paramN_type paramN)
AND note that the function body, here, is
{
stmt;
stmt;
...
stmt;
return expr;
}
* NOTE: in Racket, the value of the function is the value of the
expression (really the "final" expression) in the function body,
although we never got around to having more than one...!)
* in C++, the value of a function is determined with a
return statement:
return <expr>; <--- note the semicolon!
...the function is DONE when a return statement is
reached, and it returns the value of the <expr>.
* NOW: does it matter where you put the { } and the body in
C++?
... to the compiler? NO. It is not syntactically significant.
BUT!!!! I care a great deal about indentation and white
space to enhance readability...
CS 111 STYLE RULES: (ONLY the beginning... 8-) )
1. { and } on their own lines (as shown above)
2. thou SHALT indent the statements within { and } at
least 3 spaces
* in Racket, you define a function in a definitions window,
run that window, and use that function
...in C++, you run a C++ program, which consists of as
many functions as you want, one of which is named main
...when you execute the program, main function starts up.
...but we'll use funct_play and funct_compile to play with
non-main C++ functions BEFORE dealing with main.
* (we then walked through using funct_play to write, compile,
test, and run a C++ non-main function, STILL using the
design recipe!!)