Chapter 1:
Introduction to Computers,
Programs, and C++
2
What is a Computer?
A computer consists of a CPU, memory, hard disk, monitor,
printer, and communication devices.
CPU
e.g., Disk, CD,
and Tape
Input
Devices
e.g., Keyboard,
Mouse
e.g., Monitor,
Printer
Communication
Devices
e.g., Modem,
and NIC
Storage
Devices
Memory Output
Devices
Bus
3
CPU
The central processing unit (CPU) is the brain of a computer.
It retrieves instructions from memory and executes them.
The CPU speed is measured in megahertz (MHz),
with 1 megahertz equaling 1 million pulses per second.
The speed of the CPU has been improved continuously
CPU
e.g., Disk, CD,
and Tape
Input
Devices
e.g., Keyboard,
Mouse
e.g., Monitor,
Printer
Communication
Devices
e.g., Modem,
and NIC
Storage
Devices
Memory Output
Devices
Bus
4
Memory
Memory is to store data and program instructions for CPU to
execute.
A memory unit is an ordered sequence of bytes,
each holds eight bits.
A program and its data must be brought to memory before they can
be executed.
The current content of a memory byte is lost whenever new
information is placed in it.
C P U
e .g . , D i s k , C D ,
a n d T a p e
I n p u t
D e v i c e s
e . g . , K e y b o a r d ,
M o u s e
e . g . , M o n i t o r ,
P r in t e r
C o m m u n i c a t i o n
D e v i c e s
e .g . , M o d e m ,
a n d N I C
S t o r a g e
D e v i c e s
M e m o r y O u t p u t
D e v i c e s
B u s
5
How Data is Stored? �Data of various kinds:
�such as numbers, characters, and strings, are encoded as a series of bits (zeros and ones). Computers use zeros and ones
�digital devices have two stable states, which are referred to as zero and one by convention.
�The programmers need not to be concerned about the encoding and decoding of data, which is performed automatically by the system based on the encoding scheme. The encoding scheme varies.
� For example, character ‘J’ is represented by 01001010 in one byte.
�A small number such as three can be stored in a single byte.
.
.
.
2000
2001
2002
2003
2004
.
.
.
01001010
01100001
01110110
01100001
00000011
Memory content
Memory address
Encoding for character ‘J’ Encoding for character ‘a’ Encoding for character ‘v’ Encoding for character ‘a’ Encoding for number 3
6
How Data is Stored?
� If computer needs to store a large number that cannot fit into a single byte, it uses a number of adjacent bytes.
� A byte is the minimum storage unit.
7
Storage Devices
Memory is volatile, because information is lost when the power is
off.
Programs and data are permanently stored on storage devices and
are moved to memory when the computer actually uses them.
There are main types of storage devices:Disk drives (hard disks and
floppy disks), CD drives (CD-R and CD-RW), and Tape drives.
CPU
e.g., Disk, CD,
and Tape
Input
Devices
e.g., Keyboard,
Mouse
e.g., Monitor,
Printer
Communication
Devices
e.g., Modem,
and NIC
Storage
Devices
Memory Output
Devices
Bus
8
Output Devices: Monitor
The monitor displays information (text and graphics).
The resolution and dot pitch determine the quality of the display.
CPU
e.g., Disk, CD,
and Tape
Input
Devices
e.g., Keyboard,
Mouse
e.g., Monitor,
Printer
Communication
Devices
e.g., Modem,
and NIC
Storage
Devices
Memory Output
Devices
Bus
9
Communication Devices A regular modem uses a phone line and can transfer data in a speed up to
56,000 bps (bits per second).
A DSL (digital subscriber line) also uses a phone line and can transfer data
in a speed 20 times faster than a regular modem.
Network interface card (NIC) is a device to connect a computer to a local
area network (LAN).
The LAN is commonly used in business, universities, and government
organizations. A typical type of NIC, called 10BaseT, can transfer data at
10 mbps (million bits per second).
CPU
e.g., Disk, CD,
and Tape
Input
Devices
e.g., Keyboard,
Mouse
e.g., Monitor,
Printer
Communication
Devices
e.g., Modem,
and NIC
Storage
Devices
Memory Output
Devices
Bus
10
ھل >B8ن A8BC9>و?ر ان >;ل ا56789ل ؟Can Computers Solve Problems?
� A computer cannot analyze a problem and come up with a solution
� A computer can only do what it is programmed to do
� A programmer must analyze the problem and develop the instructions for solving the problem and have the computer carry out the instructions
11
The Software Development Life Cycle (SDLC)
� Creating complex programs requires planning and coordination
� A software project is broken into five phases
.1� Analyze and understand the requirements of the)( ,+م و)'&%ل ا�"! �project
Devise a plan to accomplish the task)( )2"%م ا�'ل2.
96� ا�9ر67"35.(: )Create a program that implements the plan(
)Test and correct the program( ,'ص و)د=%ق ا�9ر67"45.
Maintenance)( ا�'ل و76%2� )B%%م5.
12
Programs
�A program is a sequence of instructions (in some programming language) that direct the computer’s processes to perform a specific task
�You tell a computer what to do through programs.
� Without programs, a computer is an empty machine.
� Computers do not understand human languages, so you need to use computer languages to communicate with them.
� Programs are written using programming languages.
13
Programming language
� A programming language is a set of words, symbols and grammar rules used to construct a program.
programming language
Machine Language
Assembly Language
High-Level Language
14
Machine Language
� Machine language: the instructions built into the hardware of a particular computer
� Initially, humans had no choice but to write programs in machine language because other programming languages had not yet been invented
15
Machine Language
� Every processor type has its own set of specific machine instructions
� The relationship between the processor and the instructions it can carry out is completely integrated
� Each machine-language instruction does only one very low-level task
16 16
Some Sample Instructions
Subset of Pep/7 instructions
17
A Program Example � Let’s write "Hello" on the screen
18
Assembly Language
� Assembly languages: assign mnemonic letter codes to each machine-language instruction
� The programmer uses these letter codes in place of binary digits
� A program called an assembler reads each of the instructions in mnemonic form and translates it into the machine-language equivalent
… ADD R1, R2, R3
…
Assembly Source File
Assembler
… 1101101010011010
…
Machine Code File
19
Assembly language example
� Similar to machine language, except mnemonic codes are used to make it easier for humans to read
� Example: � add two simple integers
LOAD 0237 R1 (load a number from main memory into Register “R1”) ADD 02C3 R1 (add a different number into that same Register “R1”) STOR 0F3B R1 (store the result from Register “R1” back into main memory)
20
High-Level Programming Languages
� Programs written in high-level languages must be translated into machine language
� Two methods of translation:
� Compilation (or compiling)
� Interpretation (or interpreting)
21
Compilation vs. Interpretation
� Compilation
� Translation all at once
� Like translating a book
� Interpretation
�Translation happens “on the fly”
� Like telling a story to someone who doesn’t speak your language
22
Execution of a Program. Using Compiler:
Source Code Compiler Object Code
Execute a line
of Program
Using Interpreter:
Source Code Interpreter
Execute Program
23
Execution of a Program.
Java Program:
Java
Source Code Compiler
Bytecode
program
JVM
Execute Program
24
Compiling Java Source Code
� The bytecode can run on any computer with a
Java Virtual Machine, as shown in Figure, Java Virtual Machine is a software that interprets Java bytecode
Java Bytecode
Java Virtual Machine
Any Computer
25
Popular High-Level Languages
� COBOL (COmmon Business Oriented Language)
� FORTRAN (FORmula TRANslation)
� BASIC (Beginner All-purpose Symbolic Instructional Code)
� Pascal (named for Blaise Pascal)
� Ada (named for Ada Lovelace)
� C (whose developer designed B first)
� Visual Basic (Basic-like visual language developed by Microsoft)
� Delphi (Pascal-like visual language developed by Borland)
� C++ (an object-oriented language, based on C)
� Java (We use it in the book)
26 26
Programming Languages Classifications
27
Language Syntax
� Language definition can be loosely divided into two parts: syntax, or structure; and semantics, or meaning.
� The syntax of a programming language is in many ways like the grammar of a natural language.
� The syntax of almost all languages is now given using context-free grammars.
� For example, a context-free grammar rule for the Pascal if-statement can be written as follows:
<if-statement>:: = if <condition> then <statement> [else <statement>]
28
Lexical Structure
� The lexical structure of a programming language is the structure of the words of the language, which are usually called tokens.
� The lexical structure is similar to spelling in a natural language.
29
Language Semantics
� The semantics, or meaning, of a language is much more complex and difficult to describe precisely.
� What is difficult about semantics is that the meaning of a particular mechanism may involve interactions with other mechanisms in the language.
� No generally accepted method, such as the use of context-free grammars for syntax, exists.
30
History of C++
� C++ is an extension of C, developed by Bjarne
Stroustrup at Bell Labs during 1983-1985.
� C++ added a number of features that improved
the C language.
� An international standard for C++ was created
by American National Standards Institute (ANSI)
in 1998.
31
A Simple C++ Program
Once you understand the program, it is easy to extend it to display more
messages. For example, you can rewrite the program to display three
messages
#include <iostream>
int main()
{
std::cout << "Welcome to C++!" << std::endl;
std::cout << "Welcome to C++Builder!" << std::endl;
std::cout << "Welcome to C++ Compiler!" << std::endl;
return 0;
}
32
Typical Development and Execution Environment
� C++ programs normally undergo six phases
� Edit � Programmer writes program (and stores source code on disk)
� Preprocess � Perform certain manipulations before compilation
� Compile � Compiler translates C++ programs into machine languages
� Link � Link object code with missing functions and data
� Load � Transfer executable image to memory
� Execute � Execute the program one instruction at a time
33
Typical C++
environment.