Chapter 2. Introduction to C++
description
Transcript of Chapter 2. Introduction to C++
![Page 1: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/1.jpg)
1
Starting Out with C++, 3rd Edition
Chapter 2. Introduction to C++
![Page 2: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/2.jpg)
2
Starting Out with C++, 3rd Edition
2.1 The Parts of a C++ Program
• C++ programs have parts and components that serve specific purposes.
![Page 3: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/3.jpg)
3
Starting Out with C++, 3rd Edition
Program 2-1
//A simple C++ program
#include <iostream.h>
void main (void)
{
cout<< “Programming is great fun!”;
}
Program Output:
Programming is great fun!
![Page 4: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/4.jpg)
4
Starting Out with C++, 3rd Edition
Table 2-1Character Name Description // double slash Marks the beginning of a comment. # Pound sign Marks the beginning of a preprocessor
directive < > Opening and
closing brackets
Encloses a filename when used with the #include directive
( ) Opening and closing parenthesis
Used in naming a function, as in int main ()
{ } Opening and closing braces
Encloses a group of statements, such as the contents of a function.
" " Opening and closing quotation marks
Encloses a string of characters, such as a message that is to be printed on the screen
; Semicolon Marks the end of a complete programming statement
![Page 5: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/5.jpg)
5
Starting Out with C++, 3rd Edition
2.2 The cout Object
• Use the cout object to display information on the computer’s screen. The cout object is referred to as the standard
output object. Its job is to output information using the
standard output device
![Page 6: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/6.jpg)
6
Starting Out with C++, 3rd Edition
Program 2-2
// A simple C++ program
#include <iostream.h>
void main (void)
{
cout<< “Programming is “ << “great fun!”;
}
Output:
Programming is great fun!
![Page 7: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/7.jpg)
7
Starting Out with C++, 3rd Edition
Program 2-3
// A simple C++ program#include <iostream.h>
void main (void){cout<< “Programming is “;cout << “ great fun!”;
}
Output:Programming is great fun!
![Page 8: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/8.jpg)
8
Starting Out with C++, 3rd Edition
Program 2-4// An unruly printing program
#include <iostream.h>
void main(void)
{
cout << "The following items were top sellers";
cout << "during the month of June:";
cout << "Computer games";
cout << "Coffee";
cout << "Aspirin";
}
Program Output
The following items were top sellersduring the month ofJune:Computer gamesCoffeeAspirin
![Page 9: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/9.jpg)
9
Starting Out with C++, 3rd Edition
New lines
• cout does not produce a newline at the end of a statement
• To produce a newline, use either the stream manipulator endl
• or the escape sequence \n
![Page 10: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/10.jpg)
10
Starting Out with C++, 3rd Edition
Program 2-5
// A well-adjusted printing program
#include <iostream.h>
void main(void)
{
cout << "The following items were top sellers" << endl;
cout << "during the month of June:" << endl;
cout << "Computer games" << endl;
cout << "Coffee" << endl;
cout << "Aspirin" << endl;
}
![Page 11: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/11.jpg)
11
Starting Out with C++, 3rd Edition
Program Output
The following items were top sellers
during the month of June:
Computer games
CoffeeAspirin
![Page 12: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/12.jpg)
12
Starting Out with C++, 3rd Edition
Program 2-6
// Another well-adjusted printing program
#include <iostream.h>
void main(void)
{
cout << "The following items were top sellers" << endl;
cout << "during the month of June:" << endl;
cout << "Computer games" << endl << "Coffee";
cout << endl << "Aspirin" << endl;
}
![Page 13: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/13.jpg)
13
Starting Out with C++, 3rd Edition
Program Output
The following items were top sellers
during the month of June:
Computer games
Coffee
Aspirin
![Page 14: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/14.jpg)
14
Starting Out with C++, 3rd Edition
Program 2-7
// Yet another well-adjusted printing program
#include <iostream.h>
using namespace std;
void main(void)
{
cout << "The following items were top sellers\n";
cout << "during the month of June:\n";
cout << "Computer games\nCoffee";
cout << "\nAspirin\n";
}
![Page 15: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/15.jpg)
15
Starting Out with C++, 3rd Edition
Program Output
The following items were top sellers
during the month of June:
Computer games
Coffee
Aspirin
![Page 16: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/16.jpg)
16
Starting Out with C++, 3rd Edition
Table 2-2
Table 2-2 Common Escape Sequences Escape Sequence
Name Description
\n Newline Causes the cursor to go to the next line for subsequent printing
\t Horizontal tab
Causes the cursor to skip over to the next tab stop
\a Alarm Causes the computer to beep \b Backspace Causes the cursor to back up, or
move left one position \r Return Causes the cursor to go to the
beginning of the current line, not the next line.
\\ Backslash Causes a backslash to be printed \' Single quote Causes a single quotation mark
to be printed \" Double quote Causes a double quotation mark
to be printed
![Page 17: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/17.jpg)
17
Starting Out with C++, 3rd Edition
2.3 The #include Directive
• The #include directive causes the contents of another file to be inserted into the program
• Preprocessor directives are not C++ statements and do not require semicolons at the end
![Page 18: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/18.jpg)
18
Starting Out with C++, 3rd Edition
2.5 Variables and Constants
• Variables represent storage locations in the computer’s memory. Constants are data items whose values do not change while the program is running.
• Every variable must have a declaration.
![Page 19: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/19.jpg)
19
Starting Out with C++, 3rd Edition
Program 2-8
#include <iostream.h>
void main(void)
{
int value;
value = 5;
cout << “The value is “ << value << endl;
}
Program Output:
The value is 5
![Page 20: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/20.jpg)
20
Starting Out with C++, 3rd Edition
Assignment statements:
Value = 5; //This line is an assignment statement.
• The assignment statement evaluates the expression on the right of the equal sign then stores it into the variable named on the left of the equal sign
• The data type of the variable was in integer, so the data type of the expression on the right should evaluate to an integer as well.
![Page 21: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/21.jpg)
21
Starting Out with C++, 3rd Edition
Constants
• A variable is called a “variable” because its value may be changed. A constant, on the other hand, is a data item whose value does not change during the program’s execution.
![Page 22: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/22.jpg)
22
Starting Out with C++, 3rd Edition
Program 2-10
#include <iostream.h>
void main (void)
{
int apples;
apples = 20;
cout<< “Today we sold “ << apples << “ bushels\n”;
cout << “of apples.\n”;
}
![Page 23: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/23.jpg)
23
Starting Out with C++, 3rd Edition
Program Output
Today we sold 20 bushels
of apples.
Where are the constants in program 2-10?
![Page 24: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/24.jpg)
24
Starting Out with C++, 3rd Edition
Constants from Program 2-10
Constant Type of Constant20 Integer constant"Today we sold " String constant" bushels\n" String constant"of apples.\n" String constant
![Page 25: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/25.jpg)
25
Starting Out with C++, 3rd Edition
2.6 Focus on Software Engineering: Identifiers
• Must not be a key word• Should be mnemonic (give an indication of what
the variable is used for)• The first character must be a letter or an
underscore• The remaining may be letters, digits, or
underscores• Upper and lower case letters are distinct
![Page 26: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/26.jpg)
26
Starting Out with C++, 3rd Edition
Table 2-3 The C++ Key Words
asm auto break bool case catch char class const const_cast continue default de lete do double dynamic_cast e lse enum explicit extern fa lse float for fr iend goto if in line in t long mutab le namespace new operator private protected public reg ister re in terpret_cast re turn short signed sizeof sta tic sta tic_cast struct switch template th is throw true try typedef typeid typename union unsigned using virtua l vo id vo la tile wchar_t while
![Page 27: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/27.jpg)
27
Starting Out with C++, 3rd Edition
Table 2-4 Some Variable Names
Variable Name Legal or Illegal?
dayOfWeek Legal
3dGraph Illegal. Cannot begin with a digit.
_employee_num Legal
june1997 Legal
Mixture#3 Illegal. Cannot use # symbol.
![Page 28: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/28.jpg)
28
Starting Out with C++, 3rd Edition
2.7 Integer Data Types
• There are many different types of data. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Integer variables only hold whole numbers.
![Page 29: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/29.jpg)
29
Starting Out with C++, 3rd Edition
Table 2-5
Table 2-5 Integer Data Types, Sizes and RangesData Type Size Rangeshort 2 Bytes -32,768 to +32.767unsigned short 2 Bytes 0 to +65,535int 4 Bytes -2,147,4833,648 to +2,147,4833,647unsigned int 4 Bytes 0 to 4,294,967,295long 4 Bytes -2,147,4833,648 to +2,147,4833,647unsigned long 4 Bytes 0 to 4,294,967,295
![Page 30: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/30.jpg)
30
Starting Out with C++, 3rd Edition
Program 2-11// This program has variables of several of the integer types.#include <iostream.h>
void main(void){
int checking;unsigned int miles;long days;
checking = -20;miles = 4276;days = 184086;cout << "We have made a long journey of " << miles;cout << " miles.\n";cout << "Our checking account balance is " << checking;cout << "\nExactly " << days << " days ago Columbus ";cout << "stood on this spot.\n";
}
![Page 31: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/31.jpg)
31
Starting Out with C++, 3rd Edition
Program Output
We have made a long journey of 4276 miles.
Our checking account balance is -20
Exactly 184086 days ago Columbus stood on this spot.
![Page 32: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/32.jpg)
32
Starting Out with C++, 3rd Edition
Program 2-12
// This program shows three variables declared on the same
// line.
#include <iostream.h>
void main(void)
{
int floors,rooms,suites;
floors = 15;
rooms = 300;
suites = 30;
cout << "The Grande Hotel has " << floors << " floors\n";
cout << "with " << rooms << " rooms and " << suites;
cout << " suites.\n";
}
![Page 33: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/33.jpg)
33
Starting Out with C++, 3rd Edition
Program Output
The Grande Hotel has 15 floors
with 300 rooms and 30 suites.
![Page 34: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/34.jpg)
34
Starting Out with C++, 3rd Edition
Hexadecimal and Octal Constants
• Hexadecimal numbers are preceded by 0x Hexadecimal F4 would be expressed in C++ as
0xF4
• Octal numbers are preceded by a 0 Octal 31 would be written as 031
![Page 35: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/35.jpg)
35
Starting Out with C++, 3rd Edition
2.8 The char Data Type
• Usually 1 byte long
• Internally stored as an integer
• ASCII character set shows integer representation for each character
• ‘A’ == 65, ‘B’ == 66, ‘C’ == 67, etc.
• Single quotes denote a character, double quotes denote a string
![Page 36: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/36.jpg)
36
Starting Out with C++, 3rd Edition
Program 2-13
// This program demonstrates the close relationship between
// characters and integers.
#include <iostream.h>
void main(void)
{
char letter;
letter = 65;
cout << letter << endl;
letter = 66;
cout << letter << endl;
}
![Page 37: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/37.jpg)
37
Starting Out with C++, 3rd Edition
Program Output
A
B
![Page 38: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/38.jpg)
38
Starting Out with C++, 3rd Edition
Program 2-14
// This program uses character constants
#include <iostream.h>
void main(void)
{
char letter;
letter = 'A';
cout << letter << endl;
letter = 'B';
cout << letter << endl;
}
![Page 39: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/39.jpg)
39
Starting Out with C++, 3rd Edition
Program Output
A
B
![Page 40: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/40.jpg)
40
Starting Out with C++, 3rd Edition
C-Strings
• C-Strings are consecutive sequences of characters and can occupy several bytes of memory.
• C-Strings always have a null terminator at the end. This marks the end of the string.
• Escape sequences are always stored internally as a single character.
![Page 41: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/41.jpg)
41
Starting Out with C++, 3rd Edition
Program 2-15// This program uses character constants#include <iostream.h>
void main(void){
char letter;
letter = 'A';cout << letter << '\n';letter = 'B';cout << letter << '\n';
}
![Page 42: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/42.jpg)
42
Starting Out with C++, 3rd Edition
Program Output
A
B
![Page 43: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/43.jpg)
43
Starting Out with C++, 3rd Edition
Review key points regarding characters, strings, and C-strings:• Printable characters are internally represented by numeric codes. Most
computers use ASCII codes for this purpose.
• Characters occupy a single byte of memory.
• C-Strings are consecutive sequences of characters and can occupy several bytes of memory.
• C-Strings always have a null terminator at the end. This marks the end of the C-string.
• Character constants are always enclosed in single quotation marks.
• String constants are always enclosed in double quotation marks.
• Escape sequences are always stored internally as a single character.
![Page 44: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/44.jpg)
44
Starting Out with C++, 3rd Edition
2.9 The C++ string Class
• The string class is an abstract data type.
• It provides capabilities that make working with strings easy and intuitive.
• You must us the #include <string> statement.
![Page 45: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/45.jpg)
45
Starting Out with C++, 3rd Edition
2.10 Floating Point Data Types
• Floating point data types are used to declare variables that can hold real numbers
![Page 46: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/46.jpg)
46
Starting Out with C++, 3rd Edition
Table 2-7Table 2-7 Floating Point Data Types on PCs Data Type Key Word Description
Single Precision float 4 bytes. Numbers between + 3.4E-38 and + 3.4E38
Double Precision double 8 bytes. Numbers between +1.7E-308 and +1.7E308
Long Double Precision
long double 8 bytes. Numbers between +1.7E-308 and +1.7E308
![Page 47: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/47.jpg)
47
Starting Out with C++, 3rd Edition
Program 2-17
// This program uses floating point data types
#include <iostream.h>
void main(void)
{
float distance;
double mass;
distance = 1.495979E11;
mass = 1.989E30;
cout << "The Sun is " << distance << " kilometers away.\n";
cout << "The Sun\'s mass is " << mass << " kilograms.\n";
}
![Page 48: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/48.jpg)
48
Starting Out with C++, 3rd Edition
Program Output
The Sun is 1.4959e+11 kilometers away.
The Sun's mass is 1.989e+30 kilograms.
![Page 49: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/49.jpg)
49
Starting Out with C++, 3rd Edition
Floating Point Constants
• May be expressed in a variety of ways
• E notation
• decimal notation
• (no commas)
![Page 50: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/50.jpg)
50
Starting Out with C++, 3rd Edition
2.11 The bool Data Type
• Boolean variables are set to either true or false
![Page 51: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/51.jpg)
51
Starting Out with C++, 3rd Edition
Program 2-18#include <iostream.h>
void main (void)
{
bool boolValue;
boolValue = true;
cout << boolValue << endl;
boolValue = false;
cout << boolValue << endl;
}
![Page 52: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/52.jpg)
52
Starting Out with C++, 3rd Edition
Program Output
1
0
Internally, true is represented as the number 1 and false is represented by the number 0.
![Page 53: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/53.jpg)
53
Starting Out with C++, 3rd Edition
2.12 Focus on Software Engineering: Determining the Size of a Data Type
• The sizeof operator may be used to determine the size of a data type on any system.
cout << “The size of an integer is “ << sizeof(int);
![Page 54: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/54.jpg)
54
Starting Out with C++, 3rd Edition
Program 2-19
#include <iostream.h>
void main (void)
{
long double apple;
cout << “The size of an integer is “ << sizeof(int);
cout << “ bytes.\n”;
cout << “The size of a long integer is “ << sizeof(long);
cout << “ bytes.\n”;
cout << “An apple can be eaten in “ << sizeof(apple);
cout << “ bytes!\n”;
}
![Page 55: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/55.jpg)
55
Starting Out with C++, 3rd Edition
Program Output
The size of an integer is 4 bytes.
The size of a long integer is 4 bytes.
An apple can be eaten in 8 bytes!
![Page 56: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/56.jpg)
56
Starting Out with C++, 3rd Edition
2.13 Focus on Software Engineering: Variable Assignment and Initialization
• An assignment operation assigns, or copies, a value into a variable. When a value is assigned to a variable as part of the variable’s declaration, it is called an initialization.
![Page 57: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/57.jpg)
57
Starting Out with C++, 3rd Edition
Program 2-20
#include <iostream.h>
void main (void)
{
int month = 2, days = 28;
cout << “Month “ << month << “ has “ << days << “ days.\n”;
}
Program output:
Month 2 has 28 days.
![Page 58: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/58.jpg)
58
Starting Out with C++, 3rd Edition
2.14 Focus on Software Engineering: Scope
• A variable’s scope is the part of the program that has access to the variable.
• A variable must be declared before it is used.
![Page 59: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/59.jpg)
59
Starting Out with C++, 3rd Edition
Program 2-21
// This program can't find its variable
#include <iostream.h>
void main(void)
{
cout << Value;
int Value = 100;
}
![Page 60: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/60.jpg)
60
Starting Out with C++, 3rd Edition
2.15 Arithmetic Operators
• There are many operators for manipulating numeric values and performing arithmetic operations.
• Generally, there are 3 types of operators: unary, binary, and ternary. Unary operators operate on one operand Binary operators require two operands Ternary operators need three operands
![Page 61: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/61.jpg)
61
Starting Out with C++, 3rd Edition
Table 2-8Table 2-8 Fundamental Arithmetic OperaotrsOperator Meaning Type Example
+ Addition Binary Total = Cost + Tax;- Subtraction Binary Cost = Total - Tax;* Multiplication Binary Tax = Cost * Rate;/ Division Binary SalePrice = Original/2;
% Modulus Binary Remainder = Value%3;
![Page 62: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/62.jpg)
62
Starting Out with C++, 3rd Edition
Program 2-22// This program calculates hourly wages// The variables in function main are used as follows:// regWages: holds the calculated regular wages.// basePay: holds the base pay rate.// regHours: holds the number of hours worked less overtime.// otWages: holds the calculated overtime wages.// otPay: holds the payrate for overtime hours.// otHours: holds the number of overtime hours worked.// totalWages: holds the total wages. #include <iostream.h>
void main(void){
float regWages, basePay = 18.25, regHours = 40.0;float otWages, otPay = 27.78, otHours = 10;float totalWages;
regWages = basePay * regHours; otWages = otPay * otHours;totalWages = regWages + otWages;cout << "Wages for this week are $" << totalWages << endl;
}
![Page 63: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/63.jpg)
63
Starting Out with C++, 3rd Edition
2.16 Comments
• Comments are notes of explanation that document lines or sections of a program.
• Comments are part of a program, but the compiler ignores them. They are intended for people who may be reading the source code.
• Commenting the C++ Way
//
• Commenting the C Way
/* */
![Page 64: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/64.jpg)
64
Starting Out with C++, 3rd Edition
Program 2-23// PROGRAM: PAYROLL.CPP
// Written by Herbert Dorfmann
// This program calculates company payroll
// Last modification: 3/30/96
#include <iostream.h>
void main(void)
{
float payRate; // holds the hourly pay rate
float hours; // holds the hours worked
int empNum; // holds the employee number
(The remainder of this program is left out.)
![Page 65: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/65.jpg)
65
Starting Out with C++, 3rd Edition
Program 2-24 // PROGRAM: PAYROLL.CPP
// Written by Herbert Dorfmann
// Also known as "The Dorfmiester"
// Last modification: 3/30/96
// This program calculates company payroll.
// Payroll should be done every Friday no later than
// 12:00 pm. To start the program type PAYROLL and
// press the enter key.
#include <iostream.h> // Need the iostream file because
// the program uses cout.
void main(void) // This is the start of function main.
{ // This is the opening brace for main.
float payRate; // payRate is a float variable.
// It holds the hourly pay rate.
float hours; // hours is a float variable too.
// It holds the hours worked.
int empNum; // empNum is an integer.
// It holds the employee number.
(The remainder of this program is left out.)
![Page 66: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/66.jpg)
66
Starting Out with C++, 3rd Edition
Program 2-25
/*
PROGRAM: PAYROLL.CPP
Written by Herbert Dorfmann
This program calculates company payroll
Last modification: 3/30/96
*/
#include <iostream.h>
void main(void)
{
float payRate; /* payRate holds hourly pay rate */
float hours; /* hours holds hours worked */
int empNum; /* empNum holds employee number */
(The remainder of this program is left out.)
![Page 67: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/67.jpg)
67
Starting Out with C++, 3rd Edition
Program 2-26
/*
PROGRAM: PAYROLL.CPP
Written by Herbert Dorfmann
This program calculates company payroll
Last modification: 3/30/96
*/
#include <iostream.h>
void main(void)
{
float payRate; // payRate holds the hourly pay rate
float hours; // hours holds the hours worked
int empNum; // empNum holds the employee number
(The remainder of this program is left out.)
![Page 68: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/68.jpg)
68
Starting Out with C++, 3rd Edition
2.17 Focus on Software Engineering: Programming Style
• Program style refers to the way a programmer uses identifiers, spaces, tabs, blank lines, and punctuation characters to visually arrange a program’s source code.
Generally, C++ ignores white space. Indent inside a set of braces. Include a blank line after variable declarations.
![Page 69: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/69.jpg)
69
Starting Out with C++, 3rd Edition
Program 2-27
#include <iostream.h>
void main(void){float shares=220.0;float avgPrice=14.67;cout <<"There were "<<shares<<" shares sold at $"<<avgPrice<<" per share.\n";}
Program Output
There were 220 shares sold at $14.67 per share.
![Page 70: Chapter 2. Introduction to C++](https://reader031.fdocuments.us/reader031/viewer/2022020920/56814e2f550346895dbb946b/html5/thumbnails/70.jpg)
70
Starting Out with C++, 3rd Edition
Program 2-28
// This example is much more readable than Program 2-26.
#include <iostream.h>
void main(void)
{
float shares = 220.0;
float avgPrice = 14.67;
cout << "There were " << shares << " shares sold at $";
cout << avgPrice << " per share.\n";
}
Program Output
There were 220.0 shares sold at $14.67 per share.