C++ Seminar: 1094 Session 2
Handouts: (Pick up handouts from Session 1 if you missed it.)
Your goals for today: Quick
review of Days 1 and 2 then work through Days 3 and 4.
Review: Days 1 and 2
We'll briefly step through the C++ features used in Days 1 and 2,
prompting for questions.
- The std "namespace"
- Imagine that you wanted to define your own versions of cout,
cin, and endl. How would they be distinguished from
the "official" C++ versions? Answer: You would have your own namespace,
such as mystuff. The official version would be
std::cout while your version would be mystuff::cout.
[Note: Details on creating a namespace like this are in Day 18.]
- If there's no chance of confusion, you might get annoyed at typing
std:: all the time. Then the command using std::cout;
will tell C++ to interpret cout as the official version.
If you said using mystuff::cout; then cout would
refer to your version.
- If you put using namespace std; or using std::cout;
before any of the functions in the file,
they will apply to all of the functions in that file.
If you put them inside of a function, they apply only to that function.
- cout and cin
- These are defined in the "iostream" library, so you'll get an error
about them being undefined or unrecognized if you forget
#include <iostream>
or if you don't use std::.
- The "stream" flows into cout (indicated by <<)
and out of cin (indicated by >>). Think of these
as
abbreviations for "console output" and "console input".
- \n means start a new line and \t means to
tab (skip 8 spaces on most computers).
- Two types of comments: // and /* */
- Functions
- Every function has a return type, which is given just
before the function
name when the function is defined,
and each of the arguments also has a type. For example:
int Add (int first, int second)
Some of the types you'll encounter in Day 3 are int (integer),
char (character), float (single precision floating
point number), and double (double precision floating point
number).
- Dummy arguments: The arguments of a function are just like
dummy variables in an integration. These two functions are identical:
int Subtract (int a, int b)
{
return (a - b);
}
and
int Subtract (int first, int second)
{
return (first - second);
}
However, the choice of variable names can make your function
easier to understand and debug.
Day 3: Working with Variables and Constants
You can find more information about these things in the
online version of Day 3.
- The basic unit for storing information in the computer is the "byte",
which is equal to 8 bits (a bit is a 0 or a 1). Download, compile, and run
List0301.cpp to see the size in bytes of
each of the variable types (you could get different answers on a
supercomputer). What is the difference between a float
and a double? For scientific work, an integer is almost always
declared as an int (without long or short)
and numbers with decimal points are almost always declared as
doubles. So don't use float unless you have a really
good reason (can you guess why?).
- Variables are demonstrated in List0302.cpp. The keyword
unsigned means that the integer is only positive or zero.
Note that a value is assigned to Width when it is declared
(we say it is "initialized") but the value of Length is
assigned on a separate line. In general, you should always initialize
variables to something (often zero), but you can always re-assign the
value.
Modify the program to add a variable Depth and to calculate
the volume. However, don't delete the lines that define and print
Area, but "comment them out" using /* and
*/.
- In the last program, why not use W and L as
variable names instead of Width and Length, which
take longer to type? Try changing Width to width
in the line where it is printed out. Why does this fail?
- The use of typedef is demonstrated in
List0303.cpp. It is a way to define your own name (i.e., an
alias) for a variable type.
In this example, USHORT replaces unsigned short int.
We'll see more interesting uses for typedef later!
- Another variable type is char, which is short for
"character". Each of the standard characters, i.e., the letters,
numbers, and punctuation symbols, has an integer associated with it.
A very common set of integer associations is the ASCII code
(pronounced "Ask-ee").
Download, compile, and run List0306.cpp to see how the
characters associated with the numbers from 32 to 127.
The statement (char) i converts the integer i to
the corresponding ASCII character. It is unlikely you will need
to explicitly use the ASCII code; this is for cultural enrichment!
Try changing 127 to a larger number (but less than 256).
- Here are
some of the Day 3 Quiz questions to try
(answers are in the handout from Session 1):
- What is the difference between an integer variable and a floating-point
variable?
- What makes for a good or bad variable name?
- Which of the following variable names are good, which are bad, and
which are invalid?
Age, !ex,
R79J,
TotalIncome, __Invalid
Day 4: Creating Expressions and Statements
Your goal is to learn about blocks, expressions, how to
"branch" your code with "if" statements, and how to handle the truth.
You can find more information about these things in the online version of Day 4.
- Take a look at List0403.cpp, which demonstrates "prefix"
and "postfix" increment and decrement operators. If I set
int x = 3, then x++ or ++x will both result
in the value of x changed to 4. It doesn't matter which you
use if the statement is on a line by itself. However, if they are
part of a more complicated statement (like the cout statements
in the example), there is a difference. Can you figure out the rule?
The bottom line here is that the behavior can be quite confusing, so
the best rule is: ONLY use x++ on a line by itself.
Why do you think the language is called "C++"?
- Try replacing ++ by -- in the previous example.
What is the rule? Another way to add one to a variable is
x += 1. This generalizes trivially to adding two: x +=
2 or to multiply by two: x *= 2. Test these out.
- The example in List0404.cpp demonstrates the use of if
statements. The basic form is
if (expression)
{
statement1;
statement2;
statement3;
}
where expression is something that is either true or false
and the statements can be any C++ statements (if there is only one,
you can omit the {}'s, but we advise using them in any case).
In the example, there are comparisons of scores. It's probably easy to
interpret > and <, but if you want to check if two
numbers are equal, you use == and NOT =
(using = in this case is one of the most common beginner's
error in C or C++). Can you trace the logic of the program?
- The example in List0405.cpp introduces the else clause
(if "expression" is true do this, else do that). Try adding another
if-else combination that checks if the numbers are equal.
If they are, print "They are equal" and if not, print "They are not
equal."
- The example in List0406.cpp shows a more complicated example.
The interesting new feature here is the % operator. The
expression x % y is true if y evenly divides
x and false otherwise (so it is the modulo operator).
Try it out!
- There are C++ operators corresponding to the logical operators
AND, OR, and NOT. The symbols are &&, ||, and
! respectively. Here are two possible uses:
if ( (x == 1) && (y == 2) )
{
cout << " x is 1 and y is 2 " << endl;
}
if ( (x == 1) || (y == 2) )
{
cout << "x is 1 OR y is 2 " << endl;
}
Try these out in one of the examples.
- Here are some
of the Day 4 Quiz questions to try
(answers are in the handout from Session 1):
- What is the value of 201/4?
- What is the value of 8+2*3?
- What is the difference between if (x=3) and
if (x==3)?
- Download Ex0402.cpp but don't compile it yet. First look
at the program, imagine entering three numbers, and by following through the
"if" statements, write down what output you predict. Then compile and run it,
and check your result. If you got it wrong, go back and try again.
- Download Ex0404.cpp but don't compile it yet. Look at the
program and anticipate the output. Then check.
C++ Seminar: 1094 Session 2.
Last modified: 10:48 pm, November 05, 2006.
furnstahl.1@osu.edu