Chapter2 Introduction To C++ Programming
Chapter2 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
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
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#
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
a stream manipulator
12
13
14
15
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
17
18
19
20
21
22
23
26
27
28
A location with bool data type can be initialized with keywords true or false
Typical initialization values
0 0.0 true, false
29
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
32
33
34
35
Declaring a Variable
36
37
38
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
42
Summary
Programs have variables, constants (named, literal), and arithmetic operators (to perform calculations)
const dataType constantName = value; dataType variableName [= initialValue];