Professional Documents
Culture Documents
Introduction to C++
Starting Out with C++ Early Objects
Seventh Edition
2-1
2.1 Parts of a C++ Program
comment
// sample C++ program
preprocessor directive
#include <iostream>
which namespace to use
using namespace std;
beginning of function named main
int main() beginning of block for main
{
output statement
cout << "Hello, there!";
send 0 back to operating system
return 0;
} end of block for main
2-2
Special Characters (pg. 30)
Total is 49
2-6
Starting a New Line
2-7
New lines of output
endl – a command
• Causes a new line of output
• Dumps the output buffer to output file
‘\n’ – a special character
• Causes a new line of output only
• MUST be inside quotes
• Side-effect: when used with file output,
some output may not get printed 2-8
Test question
Common Escape Sequences (p.35)
~~ MUST be contained in quotes
~~ Considered a single character
\n Newline
\t Tab
\a Alarm
\b Backspace
\r Return (same line)
\\ Prints 1 \
\’ Prints single quote
\” Prints double quote 2-9
Examples
cout << “\’Hello\\\n Mary\’”;
‘Hello\
Mary’
total_Sales Yes
2-18
2.7 Integer Data Types
2-32
What if a variable is not initialized??
double amount;
cout << amount;
What happens?? …. It depends…
*Ignore - continues, repeats message
*Abort – program stops
*Retry – tries again then stops
- get MS “send error report?”
2-33
2.14 Scope
• The scope of a variable is that part of the
program where the variable may be used
• Scope of variable begins with definition,
continues through the block in which it is
defined
• A variable cannot be used before it is defined
int a;
cin >> a; // legal
cin >> b; // illegal
int b; 2-34
• (Test question)
Scope
To avoid problems…
• Define ALL variables & constants at the
beginning of the program!!!
• Later we will discuss other issues
related to scope.
2-35
2.15 Arithmetic Operators
2-36
Binary Arithmetic
Operators
SYMBOL OPERATION EXAMPLE ans
+ addition ans = 7 + 3; 10
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
2-39
2.16 Comments
• Used to document parts of program
• Written for persons reading source code
of program
– Indicate purpose of program
– Describe use of variables
– Explain complex sections of code
• Ignored by compiler
• REQUIRED for all programs (by RH)
• No absolute rules, but STANDARDS 2-40
Single-Line Comments
2-41
Multi-Line Comments
• Begin with /* & end with */
• Can span multiple lines
/*----------------------------
Here's a multi-line comment
----------------------------*/
• Can be used as single-line comments
int area; /* Calculated area */
• MUST have both
2-42
Comment Guidelines
• Use your textbook as a GOOD Example
• Always comment with name, project name,
project description at beginning of every
program
• Descriptive comments within body of program
are REQUIRED
– If comment is so generic it could be moved to any
other program then it is not a “good comment”
– If comment states exactly what the codes says
then it is not a “good comment”
2-43
Chapter 2 Homework
• Checkpoints
– These are at the end of most sections (e.g. page
31). You should do all of these
• End of Chapter questions –
– Page 70+; All (1 – 27) 8E
• Quizzes will usually come from checkpoints &
end of chapter questions
2-44