Programming With C++ Chapter01-2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 68

1-1

Putting It All Together

There are many software development systems


that provide an Integrated development
environment (IDE).

An IDE has the following:


• text editor
• compiler
• debugger
Programming I
with C++
1-3

Computer – programmable machine designed to


follow instructions
1-4

Program/Software – instructions that a computer


follows to perform a task
1-5

Programmer – person who designs, creates, and


tests programs for computers
Exciting and rewarding careers in CS

Business
Medicine
Government
Law Enforcement
Agriculture
Academics
Entertainment
1-7

Artistry in programming:
• Organization of the tasks that the program performs
• How information is displayed
• How the user interacts with the program
1-8

Science in programming:
• Understanding of the language used to write the program
• Understanding how to test the program, and to change it if it
does not work as intended
ENIAC
▪World’s first programmable
computer
▪ Built in 1945
▪ Designed to calculate artillery
ballistic tables for the U.S. Army
▪ CPU was 8 feet tall, 100 feet long,
and weighed 30 tons

Microprocessor
▪ Much smaller
▪ Much more powerful

1-9
1-10

Hardware – Physical components of a computer


Categories
1. Central Processing Unit (CPU)
2. Main memory (RAM)
3. Secondary storage devices
4. Input Devices
5. Output Devices
1-11
1-12

Central Processing Unit (CPU)

Hardware component that runs programs

Running or executing a program means


the computer performs the tasks that the
program tells it to do.
1-13

Main Memory
• Holds both program instructions and data
• Volatile – erased when the program terminates, or
computer is turned off
• Also called Random Access Memory (RAM), because
the CPU can access data and instructions from any
memory location.
1-14

Main Memory Organization


• Bit
–Smallest piece of memory
–Stands for binary digit
–Hold an electrical charge
▪A positive charge is “on”
▪ A negative charge is “off”

• Byte
–Is 8 consecutive bits
–Has an address in memory
–There are billions of bytes of memory in a computer
1-15

Secondary Storage

• Holds data when the program is not


running or when the computer is turned off

• Several forms of secondary storage


– disk drive: can be mounted inside the computer
or connected to an external port. Data is
stored magnetically or in solid-state memory.
– flash: SD memory card, USB flash drive
– optical: CD, DVD, Blu-ray
CPU vs RAM vs Secondary Storage
1-17

Input Devices

• Used to send information to the computer from


outside

• Many devices can provide input


– keyboard, mouse, touch screen, microphone, scanner,
digital camera, disk drive, CD/DVD drive, USB flash
drive
1-18

Output Devices

• Used to send information from the computer to


the outside

• Many devices can be used for output


– Computer screen, printer, speakers, disk drive,
CD/DVD recorder, USB flash drive
Software falls into two
1-19

categories

• System software
– programs that manage the computer hardware and the
programs that run on the computer

– Operating Systems
▪Controls the operation of the computer
▪ Manages connected devices and access to storage devices
▪ Allows programs to run
– Utility Programs
▪ Support programs that enhance computer operations
▪ Examples: virus scanners, data backup, file compression
– Software development tools
▪ Used by programmers to create software
▪ Examples: compilers, integrated development environments
(IDEs)
1-20

Software falls into two categories


• Application software
– programs that provide services to the user.
– Examples : word processing, games
How Computers Store Data
Storing Numbers
• The positive charge or the on position is represented by the digit 1

• The negative charge or the off position is represented by the digit 0

• This corresponds to the binary numbering system where all numeric values are written as a
sequence of 0s and 1s

1-21
How Computers Store Data

Storing Numbers

1-22
How Computers Store Data

Storing Numbers

• The largest value that can be stored in a byte with eight bits is 255

• Two bytes are used for larger numbers; maximum value is 65535

1-23
How Computers Store Data

Storing Characters
• Characters are stored in the computer’s memory as a
binary number

• ASCII (American Standard Code for Information


Interchange) is a coding scheme

1-24
1-25
How Computers Store Data

Storing Characters
• ASCII is a set of 128 numeric codes
• ASCII is limited

• Unicode is a more extensive encoding scheme


• It is compatible with ASCII (first 128 codes are
from the original ASCII table)
• It represents characters for many languages in
the world

1-26
Unicode

1-27
CPU is designed to perform the following operations:

• Read a piece of data from main memory


• Adding two numbers
• Subtracting one number from another number
• Multiplying two numbers
• Dividing one number by another number
• Moving a piece of data from one memory location to another
• Determining whether one value is equal to another value

What language do you think a CPU understands?


1-29

• Program
a set of instructions directing a computer to perform a task

• Programming Language
a special language used to write programs
1-30

Algorithm: a set of well-defined steps to perform a


task or to solve a problem

Order is important. Steps must be performed sequentially


Suppose we want the computer to
calculate someone’s gross pay

We start with an algorithm


Example Algorithm for Calculating Gross Pay
Although the previous algorithm defines the
steps for calculating the gross pay, it is not
ready to be executed on the computer.

The computer only understands object code


or machine language instructions
• Machine language instructions are binary numbers, such as

1011010000000101

• Rather than writing programs in machine language,


programmers use programming languages (more like
English)
1-36

Types of languages

– Low-level: used for


communication with
computer hardware directly.
Not very easy for a person
to read.

– High-level: closer to human


language
High vs Low level (Think of a recipe)

– Low Level (machine language)


▪Pick up a knife, place the knife on the apple, apply
pressure, …

– Medium Level
▪ Slice 4 apples into one-inch squares...

– High Level (high-level languages: C++)


▪ Make an apple pie
•High-level languages allow you to create powerful and
complex programs without knowing how the CPU works,
using words that are easy to understand.

1-38
The statements written in a high-level language are called
source code or simply code

1-39
Compilation – How to we get from the left 1-40

box to the right box?

#include <iostream> 01101011


using namespace std; 10100010
10011100
int main(){ .
cout<<“Hello World”<<endl; .
return 0; .
} 01010001
•Compiler is a program that translates a high-level
language program into a separate machine language
program (or object code)

1-41
1-42

From a High-level Program to an Executable File

a) Create a file containing the program with a text editor.


• program statements: source code
• file: source file

b) Run the compiler to convert source program


statements into machine instructions (object code)
1-43
Mistakes compilers catch and don’t catch:

•Syntax error is a mistake such as a:


• Misspelled word
• Missing punctuation character
• Incorrect use of an operator

1-44
Mistakes compilers catch and don’t catch:

•Logical error is an error in your own logic (compiler


does not catch these – errors occur at run time)
• Adding instead of subtracting
• Dividing by zero

1-45
Some Well-Known Programming Languages
Programming Languages
• FORTRAN - Scientific/Engineering (1957)
• COBOL - business oriented language (1959)
• C - applications and systems, dominated much of the eighties
and early nineties. (K&R published 1978)
• C++ - applications and systems, dominated much of the late
nineties. (1980-1985)
• Java - applications and network/web-based applications,
rapidly gaining popularity. (1995)
• Lisp and Prolog - Languages for AI (1959, 1970)
• Pascal - language used to teach programming (1971)
• Ada - language used in military applications (1983)
• Basic - language used to teach programming (1965)
What is C++? 1-48

• Cross-platform language used to create high-


performance applications

• Developed by Bjarne Stroustrup, as an extension to


the C language

• Gives programmers a high level of control over


system resources and memory

• Was updated 3 major times in 2011, 2014, and 2017


to C++11, C++14 and C++17
Why use C++? 1-49

• One of the world’s most popular languages

• Found in today’s operating systems, Graphical User


Interfaces, and games (CounterStrike, Doom, Red Dead
Redemption for examples)

• Is an object-oriented programming language

• Close to C# and Java making it easy to switch to it


Integrated Development Environments (IDEs)
1-51

What Is a Program Made Of?

There are common


elements in most
programming languages

–Key Words
–Programmer-Defined
Identifiers
–Operators
–Punctuation
–Syntax
Key Words

• Also known as reserved words

• Have a special meaning in C++

• Can not be used for any other purpose

• Written in lower case letters


Key Words
Programmer-Defined Identifiers

• Names made up by the programmer

• Not part of the C++ language

• Used to represent various things: variables


(memory locations)
Identifiers
Operators

• Used to perform operations on data

• Many types of operators:


–Arithmetic: +,-,*,/
–Assignment: =
Operators
Punctuation

Characters that mark the end of a


statement, or that separate items in a list
Punctuation
Syntax

• The rules of grammar that must be followed


when writing a program
• Controls the use of key words, operators,
programmer-defined symbols, and punctuation
1-61

Variables

• A variable is a named location in computer memory.

• A program with a variable named “length” is simply a


symbolic name used in your program to refer to an actual
memory location in RAM
1-62

Variables

• Holds a piece of data. The data that it holds may change


while the program is running.
1-63

Variables

There are two general types of data: numbers and


characters.
Numbers are used to perform mathematical operations,
and characters are used to print data on the screen or
on paper
1-64

Variables

• Variables must be declared or defined first:


–1st part 🡪 datatype (what will this variable store?)
–2nd part 🡪 names of the variables

double length, width, height;


1-65

Three steps that many programs perform


1) Input data
- from keyboard or mouse
- from files on disk drives

2) Process the input data

3) Output the results of the processing


- send it to the screen or a printer
- write it to a file
1-66

The Programming Process

1. Define what the program is to do.


2. Visualize the program running on the computer.
3. Use design tools to create a model of the program.
Hierarchy charts, flowcharts, pseudocode, etc.
4. Check the model for logical errors.
5. Write the program source code.
6. Compile the source code.
1-67

The Programming Process


7. Correct any errors found during compilation.
8. Link the program to create an executable file.
9. Run the program using test data for input.
10. Correct any errors found while running the
program.
Repeat steps 4 - 10 as many times as necessary.
11. Validate the results of the program.
Does the program do what was defined in step 1?
Procedural and Object-Oriented Programming

• Procedural programming: data and methods separate,


easier to learn fundamentals, code not reusable

• Object-Oriented programming: focus is on objects,


which contain data and the means to manipulate the
data. Code reusable.

You might also like