Please send questions to
st10@humboldt.edu .
INTRO to C++
* *Some* notes from the Intro to C++ discussion
(most of these created *after-the-fact* for posting,
since most of actual discussion was done "verbally")
(note: does NOT cover everything covered in lecture today,
not does it cover things in the same "order"...)
* both Racket and C++ are high-level languages ---
the computer cannot run them directly, they have
to be translated into machine-level commands
TWO of the major ways to translating high-level languages:
* interpreting
* compiling
* (and there are interesting agglomerations of these two as
well!)
* interpreting: translate ONE LINE at a time, and execute
it;
...like in the DrRacket interactions window --- you type in a line,
type enter, and that line is translated and executed;
(if you type the same line 10 times, it would be translated
and executed 10 times)
* compiling: translate a whole FILE of commands at a time,
resulting in a file that's at a "lower" level
...for C++, usually ready to be directly executed by
the machine.
* two short-term implications to note:
* compiling is all-or-nothing --- you either get a
complete translated result, or you get NOTHING
* once you have successfully compiled something,
you can run it as many times as you want without
any re-translating being necessary (so those
later runs are faster)
* (we drew the classic compiling picture on the board)
* The C++ compiler program on nrs-labs: g++
(but we'll start out having the compiler run for us by the course
C++ tools)
* yes, you have to use these tools, at least for the first few
weeks of C++
* you save Racket functions (or whatever Racket statements you'd like)
in a file whose name ends in .rkt (really, that IS a Racket
source code file, although we didn't call it that);
...you save C++ functions in a C++ source code file,
a file whose name ends in the suffix .cpp
* (okay, and so-called header information about those functions
in a header file whose name ends in .h, but more on that later)
* NOW let's get to C++ syntax, and how it compares to Racket
* Point to note: C++ is "closer to the machine" than Racket,
and it is strongly-typed -- those both lead to some of the
differences between these particular programming languages;
C++ simple expressions and types
* SIMPLE EXPRESSIONS...
* both C++ and Racket have them!
* In Racket, literals are usually simple expressions;
* In C++, literals are usually simple expressions;
* C++ is closer to the machine -- actually, integers and floating-
point numbers are stored DIFFERENTLY inside a computer;
* we just mentioned "number" as a type in Racket;
(although I believe other numeric types are actually there,
too, it just wasn't something we needed to get into);
* C++ has several numeric types, but for CS 131 we will only
care about two of them:
int - for integers
double - for double-prevision floating point numbers
C++ basic numbers:
13 <- a simple expression of C++ type int
13.0 <- a simple expression of C++ type double
(double stands for double-precision floating point)
13.1 <- is also a double
13.1111111 <- is also a double
13e5 <- is 13 times 10 to the 5th power, and that's also
of type double
* (although Racket has inexact numbers, fractional form, and
repeating decimals not supported in C++...)
C++ strings:
"foo" <-- anything in double quotes is a
C++ old-style C-string literal, of type char*
<-- string is the new-style C++ string type we will
use whenever possible! (you can't type a literal
of type string, but you CAN declare parameters
of type string, and we'll find you can use char*
string literals to help give values to string
parameters...)
C++ has booleans, too!
* the type name: bool
* the possible values: true false
C++ also has a character type, char, which represents
one character --
* you write a char literal within SINGLE quotes
'a' <-- that's the character a, its type is char
"a" <-- that's the char* string that consists of a single
character a...
'\n' <-- that's the newline char
C++ COMPOUND expressions
* C++ has a NUMBER of different kinds of compound
expressions...
* you can have 2 expressions around an INFIX
operator; some built-in infix operators in
C++ include:
+ - * /
3 + 5
% is an operator for modulo!
BUT not all infix operators are arithmetic -- there are
relational operators, too:
< > <= >= == !=
* NOTICE that you compare numbers for equality
using == not = -- = is ASSIGNMENT, not equality.
(MORE on that later!)
* != is "not equal"
AND C++ has boolean operators, too --
TRADITIONALLY, && <-- is boolean AND
|| <-- is boolean OR
! <-- is boolean NOT
BUT -- it turns out that
and
or
not
...ALSO work now! (try them in expr_play, and see!)
* there are some PREFIX operators, that go
in FRONT of an expression:
-(3 + 5)
!true
not false
* (you can put parentheses around any expression
that you want to, generally...)
* there are also a few postfix operators --
they go AFTER an expression, and more on
one of those later...