Chapter 2 Introduction to C++ Programming
Objectives
Code an algorithm into a program Desk-check a program Evaluate and modify a program Understand the components of a C++ program Create a C++ program Distinguish among a variable, a named constant, and a literal constant Explain how data is stored in memory Declare and initialize a memory location Use an assignment statement to assign data to a variable
A Brief History of Programming Languages
Programming languages are the languages used to communicate with a computer
E.g., C++, C#, Java, Visual Basic, Perl, C, COBOL Types
Machine languages Assembly languages High-level languages
Machine Languages
The first programmers had to write the program instructions using only combinations of 0s and 1s
E.g., 00101 10001 10000
Instructions written in 0s and 1s are called machine language or machine code Each type of machine has its own language Machine languages are the only way to communicate directly with the computer Programming in machine language is tedious and error-prone; requires highly trained programmers
4
Assembly Languages
Assembly languages simplify programmers job Can use mnemonics instead of 0s and 1s
E.g., ADD bx, ax
Assembly programs require an assembler to convert instructions into machine code Easier to write programs in assembly language
But still tedious and requires highly trained programmers
High-Level Languages
High-level languages allow programmer to use English-like instructions
E.g., grossPay = hours * rate High-level languages are more machine-independent
Programs written in a high-level language can be used on many different types of computers
Compilers convert the instructions into 0s and 1s Interpreters translate the program line by line as the program is running
High-Level Languages (continued)
When writing a procedure-oriented program, the programmer concentrates on the major tasks that the program needs to perform
Examples: COBOL, BASIC, C
An object-oriented program requires programmer to focus on the objects that the program can use to accomplish its goal
Examples: C++, Visual Basic, Java, C#
More on the Problem-Solving Process
Coding the Algorithm into a Program
Assigning Names, Data Types, and Initial Values to the IPO Items
To code algorithm, first assign a name to each input, processing, and output item in IPO chart
Names can contain only letters, numbers, and _ Cannot contain punctuation characters or spaces Examples:
raise newPay
usually in lowercase letters use camel case if name contains multiple words
Each input/processing/output item must have a data type You may initialize each item
10
Assigning Names, Data Types, and Initial Values to the IPO Items (continued)
double is a keyword
this is a statement
all C++ statements must end with a semicolon
11
Translating the Algorithm Steps into C++ Code
cout: standard output stream cin: standard input stream <<: insertion operator >>: extraction operator
stream: sequence of characters, to perform standard I/O operations
a stream manipulator
12
Desk-Checking the Program
13
Desk-Checking the Program (continued)
14
Desk-Checking the Program (continued)
15
Evaluating and Modifying the Program
Testing is running the program, with sample data
Results should agree with desk-check ones
Debugging is locating/removing errors in program
Program errors are called bugs
A syntax error occurs if an instruction violates the programming languages syntax (set of rules)
E.g., cout < "Hello";
A logic error occurs if an instruction does not give the expected results
E.g., average = number1 + number2 / 2;
16
Creating a C++ Program
created using an IDE or a general-purpose editor
source file: Ch3Lab2.cpp
object file: Ch3Lab2.obj
executable file: Ch3Lab2.exe
17
Creating a C++ Program (continued)
18
Variables and Named Constants
Declare a memory location for each input, processing, and output item in IPO chart
A variable is a type of memory location whose contents can change while program is running Values of named constant items remain the same each time the program is executed
19
Variables and Named Constants (continued)
Requires four memory locations: Two input items radius variable pi named constant One processing item radius squared variable One output item area variable
20
Selecting a Name for a Memory Location
Identifiers should be descriptive and follow some rules:
21
Selecting a Name for a Memory Location (continued)
Most programmers:
Use uppercase letters for named constants Use lowercase letters for variables Use camel case if a variables name contains two or more words
22
Selecting a Name for a Memory Location (continued)
23
Selecting a Data Type for a Memory Location
These data types, except string, are fundamental data types
24
Selecting a Data Type for a Memory Location (continued)
string is a class
Program must include:
#include <string> using std::string;
C++ contains one or more data types for storing
Integers (whole numbers) Floating-point numbers (with a decimal place) Characters (letters, symbols, and numbers that will not be used in calculations) Boolean values (true and false)
25
Selecting a Data Type for a Memory Location (continued)
The data type to use for a memory location depends on the values it will store
26
How Data is Stored in Internal Memory
27
How Data is Stored in Internal Memory (continued)
28
Selecting an Initial Value for a Memory Location
To initialize is to assign an initial value to a memory location
Typically a literal constant
Type can be: numeric, character, or string
A location with bool data type can be initialized with keywords true or false
Typical initialization values
0 0.0 true, false
29
Selecting an Initial Value for a Memory Location (continued)
30
Type Conversions
Implicit type conversions can occur when assigning a value to a memory location
Or, when processing calculation statements Value can be promoted or demoted
Implicit demotion can adversely affect output
Use explicit type conversion (type casting) to convert an item from one data type to another
static_cast operator
31
Type Conversions (continued)
32
Type Conversions (continued)
33
Variables and Named Constants (continued)
34
Declaring a Named Constant
35
Declaring a Variable
36
Declaring a Variable (continued)
37
Using an Assignment Statement to Store Data in a Variable
38
Using an Assignment Statement to Store Data in a Variable (continued)
39
Summary
Fourth step in the problem-solving process is to code the algorithm into a program In C++, you perform standard I/O operations using streams (sequences of characters)
cout and cin Insertion operator (<<) sends data to output stream Extraction operator (>>) gets data from input stream
After coding the algorithm, you desk-check program Final step in the problem-solving process is to evaluate and modify (if necessary) the program
40
Summary (continued)
Some programs have errors, called bugs
Syntax error occurs when an instruction violates one of the rules of the programming languages syntax Logic error occurs when you enter an instruction that does not give you the expected results You debug to locate and remove errors
To create and execute a C++ program, you need to have a text editor and a C++ compiler
Compiler translates source code into object code Linker produces executable file
41
Summary (continued)
Comments are internal documentation of programs C++ programs typically include at least one directive using statements tell compiler where it can find the definition of certain keywords A function is a block of code that performs a task
main() is where the execution of program begins
The first line in a function is called the header
After the header comes the body, enclosed in braces
42
Summary
Programs have variables, constants (named, literal), and arithmetic operators (to perform calculations)
const dataType constantName = value; dataType variableName [= initialValue];
Use assignment statement to store data in a variable
variableName = expression;
When assigning a value to a memory location, the value should fit the memory locations data type
Use static_cast operator to convert an item of data from one data type to another
Source: An Introduction to Programming with C++, Fifth Edition, Cengage Publication 43