Introduction to C++
C++ Programming: From Problem Analysis to Program Design, Eighth Edition
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain produc 1
t or service or otherwise on a password-protected website for classroom use.
Introduction
• Software is developed with programming languages
• C++ is a programming language
• C++ is suited for a wide variety of programming tasks
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 2
or otherwise on a password-protected website for classroom use.
Introduction
• Computer program
• A sequence of statements whose objective is to accomplish a task
• Programming
• The process of planning and creating a program
3 3
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
The Basics of a C++ Program
• Subprogram (or function): collection of statements
• When executed, accomplishes something
• May be predefined or standard
• Syntax rules: rules that specify which statements (instructions) are legal or valid
• Semantic rules: determine the meaning of the instructions
• Programming language: a set of rules, symbols, and special words
4 4
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Programming Methodologies
• Two popular approaches to programming design
• Structured
• Object-oriented
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 5
or otherwise on a password-protected website for classroom use.
Structured Programming
• Structured design
• Involves dividing a problem into smaller subproblems
• Structured programming
• Involves implementing a structured design
• The structured design approach is also called:
• Top-down (or bottom-up) design
• Stepwise refinement
• Modular programming
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 6
or otherwise on a password-protected website for classroom use.
Object-Oriented Programming (1 of 3)
• Object-oriented design (OOD)
• Identify components called objects
• Determine how objects interact with each other
• Specify relevant data and possible operations to be performed on that data
• Each object consists of data and operations on that data
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 7
or otherwise on a password-protected website for classroom use.
Object-Oriented Programming (2 of 3)
• An object combines data and operations on the data into a single unit
• A programming language that implements OOD is called an object-oriented
programming (OOP) language
• To design and use objects, you must learn how to:
• Represent data in computer memory
• Manipulate data
• Implement operations
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 8
or otherwise on a password-protected website for classroom use.
Object-Oriented Programming (3 of 3)
• To create operations:
• Write algorithms and implement them in a programming language
• Use functions to implement algorithms
• Learn how to combine data and operations on the data into a single unit called
a class
• C++ was designed to implement OOD
• OOD is used with structured design
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 9
or otherwise on a password-protected website for classroom use.
ANSI/ISO Standard C++
• C++ evolved from C
• C++ designed by Bjarne Stroustrup at Bell Laboratories in early 1980s
• Many different C++ compilers were available
• C++ programs were not always portable from one compiler to another
• In mid-1998, ANSI/ISO C++ language standards were approved
• Second standard, called C++11, was approved in 2011
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 10
or otherwise on a password-protected website for classroom use.
A Quick Look at a C++ Program (1 of 5)
11 11
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (2 of 5)
• Sample Run:
12 12
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Comments
• Comments are for the reader, not the compiler
• Two types
• Single line: begins with //
//**************************************************************
// Given the length and width of a rectangle, this C++ program
// computes and outputs the perimeter and area of the rectangle.
//**************************************************************
• Multiple line: enclosed between /* and */
/*
You can include comments that can
occupy several lines.
*/
13 13
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Preprocessor Directives (1 of 2)
• C++ has a small number of operations
• Many functions and symbols needed to run a C++ program are provided as
collection of libraries
• Every library has a name and is referred to by a header file
• Preprocessor directives are processed by the preprocessor program
• All preprocessor commands begin with #
• No semicolon is placed at the end of these commands
14 14
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Preprocessor Directives (2 of 2)
• Syntax to include a header file
• For example:
#include <iostream>
• Causes the preprocessor to include the header file iostream in the program
• Preprocessor commands are processed before the program goes through the
compiler
15 15
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
namespace and Using cin and cout in a Program
• cin and cout are declared in the header file iostream, but within std
namespace
• To use cin and cout in a program, use the following two statements:
#include <iostream>
using namespace std;
16 16
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Creating a C++ Program (1 of 3)
• A C++ program is a collection of functions, one of which is the function main
• The syntax of the function main used in this book has this form:
17 17
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Creating a C++ Program (2 of 3)
• Source code is comprised of preprocessor directives and program statements
• The source code file (source file) contains the source code
• The compiler generates the object code (file extension .obj)
• Executable code (file extension .exe) results when object code is linked with
the system resources
• The first line of the function main is called the heading of the function:
int main()
18 18
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Creating a C++ Program (3 of 3)
• The statements enclosed between the curly braces ({ and }) form the body of
the function
• A C++ program contains two types of statements:
• Declaration statements declare things, such as variables
• Executable statements perform calculations, manipulate data, create output, accept
input, etc.
19 19
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (5 of 5)
• Variable: a memory location whose contents can be changed
FIGURE 2-3 Memory allocation
FIGURE 2-4 Memory spaces after the statement length = 6.0; executes
20 20
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Identifiers (1 of 2)
• An identifier is the name of something that appears in a program
• Consists of letters, digits, and the underscore character (_)
• Must begin with a letter or underscore
• C++ is case sensitive
• NUMBER is not the same as number
• Two predefined identifiers are cout and cin
• Unlike reserved words, predefined identifiers may be redefined, but it is not a
good idea
21 21
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Reserved Words (Keywords)
• Reserved word symbols (or keywords):
• Cannot be redefined within a program
• Cannot be used for anything other than their intended use
• Examples include:
• int
• float
• double
• char
• const
• void
• return
22 22
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Naming Identifiers
• Identifiers can be self-documenting
• CENTIMETERS_PER_INCH
• Avoid run-together words
• annualsale
• Solutions may include:
• Capitalizing the beginning of each new word: annualSale
• Inserting an underscore just before a new word: annual_sale
23 23
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Identifiers (2 of 2)
• Legal identifiers in C++
• first
• conversion
• payRate
TABLE 2-1 Examples of Illegal Identifiers
Illegal Identifier Reason A Correct Identifier
employee Salary
There can be no space between employeeSalary
employee and Salary.
Hello!
The exclamation mark cannot be Hello
used in an identifier.
one+two
The symbol + cannot be used in an onePlusTwo
identifier.
2nd
An identifier cannot begin with a second
digit.
24 24
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Whitespaces
• Every C++ program contains whitespaces
• Include blanks, tabs, and newline characters
• Whitespaces separate special symbols, reserved words, and identifiers
• Proper utilization of whitespaces is important
• Can be used to make the program more readable
25 25
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Use of Blanks
• In C++, you use one or more blanks to separate numbers when data is input
• Blanks are also used to separate reserved words and identifiers from each other
and from other symbols
• Blanks must never appear within a reserved word or identifier
26 26
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Data Types
• A data type is set of values together with a set of allowed operations
• C++ data types fall into three categories:
• Simple data type
• Structured data type
• Pointers
27 27
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Simple Data Types (1 of 2)
• Three categories of simple data
• Integral: integers (numbers without a decimal)
- Can be further categorized: char, short, int, long, bool, unsigned char, unsigned
short, unsigned int, unsigned long
• Floating-point: decimal numbers
• Enumeration: a user-defined data type
28 28
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Simple Data Types (2 of 2)
TABLE 2-2 Values and Memory Allocation for Simple Data Types
Data Type Values Storage (in bytes)
int –147483648 (= –231 ) to 2147483647 (= 231 – 1) 4
bool true and false 1
char –128 (= –27 ) to 127 (= 27 – 1) 1
–9223372036854775808 (–263 ) to
long long 9223372036854775807(263 – 1) 64
• Different compilers may allow different ranges of values
29 29
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
char Data Type (1 of 2)
• Data type char is the smallest integral data type
• It is used for single characters: letters, digits, and special symbols
• Each character is enclosed in single quotes
• 'A', 'a', '0', '*', '+', '$', '&'
• A blank space is a character
• Written ' ', with a space left between the single quotes
30 30
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
char Data Type (2 of 2)
• Different character data sets exist
• ASCII: American Standard Code for Information Interchange
• Each of 128 values in ASCII code set represents a different character
• Characters have a predefined ordering based on the ASCII numeric value
• Collating sequence: ordering of characters based on the character set code
31 31
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Floating-Point Data Types (2 of 3)
• float: represents any real number
• Range: -3.4 * 1038 to 3.4 * 1038 (four bytes)
• double: represents any real number
• Range: –1.7 * 10308 to 1.7 * 10308 (eight bytes)
• Minimum and maximum values of data types are system dependent
32 32
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Floating-Point Data Types (3 of 3)
• Maximum number of significant digits (decimal places) for float values: 6 or 7
• Maximum number of significant digits for double: 15
• Precision: maximum number of significant digits
• float values are called single precision
• double values are called double precision
33 33
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Data Types, Variables, and Assignment Statements
• To declare a variable, must specify its data type
• Syntax rule to declare a variable is:
• dataType identifier;
• Examples include:
int counter;
double interestRate;
char grade;
• Assignment statement has the form: variable = expression
• Example: interestRate = 0.05;
34 34
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Declaring and Initializing Variables
• Not all types of variables are initialized automatically
• Variables can be initialized when declared:
int first = 13, second = 10;
char ch = ' ';
double x = 12.6;
• All variables must be initialized before they are used
• But not necessarily during declaration
35 35
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Use of Semicolons, Brackets, and Commas
• All C++ statements end with a semicolon
• Also called a statement terminator
• { and } are not C++ statements
• Can be regarded as delimiters
• Commas separate items in a list
• Declaring more than one variable following a data type
36 36
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Input (Read) Statement (1 of 3)
• cin is used with >> to gather one or more inputs
• This is called an input (read) statement
• The stream extraction operator is >>
• For example, if miles is a double variable:
cin >> miles;
• Causes the computer to get a value of type double and places it in the variable miles
37 37
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Input (Read) Statement (2 of 3)
• Using more than one variable in cin allows more than one value to be read at
a time
• Example: if feet and inches are variables of type int, this statement:
cin >> feet >> inches;
• Inputs two integers from the keyboard
• Places them in variables feet and inches respectively
38 38
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Input (Read) Statement (3 of 3)
39 39
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Output (1 of 4)
• The syntax of cout and << is:
• Called an output statement
• The stream insertion operator is <<
• Expression evaluated and its value is printed at the current cursor position on
the screen
40 40
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Output (2 of 4)
• A manipulator is used to format the output
• Example: endl causes the insertion point to move to beginning of next line
41 41
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Output (3 of 4)
• The new line character (new line escape sequence) is '\n'
• May appear anywhere in the string
• Examples
cout << "Hello there.";
cout << "My name is James.";
Output:
Hello [Link] name is James.
cout << "Hello there.\n";
cout << "My name is James.";
Output :
Hello there.
My name is James.
42 42
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
Output (4 of 4)
TABLE 2-4 Commonly Used Escape Sequences
Escape Sequence Description
\n Newline Cursor moves to the beginning of the next line
\t Tab Cursor moves to the next tab stop
\b Backspace Cursor moves one space to the left
Cursor moves to the beginning of the current line (not
\r Return the next line)
\\ Backslash Backslash is printed
\' Single quotation Single quotation mark is printed
\" Double quotation Double quotation mark is printed
43 43
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom