Programming in C++
Prerequisite: CS1 or some programming
experience
Text: C++ How to Program, Deitel & Deitel
C++ Course SarMag Trimester 3
1
Computers and Programs
• A simplified model of a computer consists
of a processing unit (CPU) and a memory.
• CPU can understand simple instructions:
–
–
–
–
read/write a memory location
add two numbers
compare numbers
etc.
C++ Course SarMag Trimester 3
2
Machine Code
• An executable program is a sequence of
these simple instructions.
• The sequence is stored in memory.
• The CPU processes the simple instructions
sequentially.
– Some instructions can tell the CPU to jump to a
new place in memory to get the next
instruction.
C++ Course SarMag Trimester 3
3
Instructions
• Each instruction is stored in memory as a
bunch of bits.
• The CPU decodes the bits to determine
what should happen.
• For example, the instruction to add 2
numbers might look like this:
10100110
C++ Course SarMag Trimester 3
4
Memory
• The Memory can hold programs and data.
• Each piece of data is also just a bunch of
bits.
• Typically the memory is organized in
chunks of 8 bits (called a byte).
• Each chunk (byte) has an address.
C++ Course SarMag Trimester 3
5
A Picture
Address
CPU
MEMORY
0
1
2
3
4
5
...
...
81345
81346
81347
...
...
C++ Course SarMag Trimester 3
6
Sample Program
#
1
2
3
4
5
6
7
8
Instruction
Set memory[801] to hold 00000001
Set memory[802] to hold 00000000
If memory[802] = 10 jump to instruction #8
increment memory[802]
set memory[803] to 2 times memory[801]
put memory[803] in to memory[801]
jump to instruction #3
print memory[801]
C++ Course SarMag Trimester 3
7
Another Picture
MEMORY
Address
CPU
0
1
2
3
4
5
Instruction #1
Instruction #2
Instruction #3
Instruction #4
Instruction #5
Instruction #6
...
...
801
802
803
C++ Course SarMag Trimester 3
8
Human vs Machine Programs
• The computer can only understand the bits (the
encoded program)
Machine Language
• Humans don't like to deal with bits, so they
developed english-like abbreviations for
programs.
Assembly Language
C++ Course SarMag Trimester 3
9
Assembly & Machine Language
Assembly Language
TOP:
BOT:
ST 1,[801]
ST 0,[802]
BEQ [802],10,BOT
INCR [802]
MUL [801],2,[803]
ST [803],[801]
JMP
TOP
LD A,[801]
CALL PRINT
Machine Language
00100101
00100100
10001010
01000100
01001000
11100101
00101001
11010101
11010100
10010001
C++ Course SarMag Trimester 3
11010011
11010100
01001001 11110000
01010100
10100111 10100011
10101011 00000010
10101000
01000100
10
An Assembler
Assembly
Language
Program
Assembler
ST 1,[801]
. . .
Machine
Language
Program
01001001
10010100
C++ Course SarMag Trimester 3
11
Higher-Level Languages
• Assembly Language is much easier to deal
with than Machine Language, but you need
to know about all the instructions.
• People developed languages that were
independent of the specific machine
language.
– More abstract representation of instructions.
C++ Course SarMag Trimester 3
12
High-Level Languages
• Many high-level languages have been
developed.
– Different ways of representing computations.
– Different languages for different needs:
•
•
•
•
symbolic vs. numeric computation
human efficiency vs. program efficiency
portability
extensibility
C++ Course SarMag Trimester 3
13
C++
• C++ is an extension of C.
• C++ was first developed in the early 1980s
(back in the last century!).
• Focus was on Object Oriented
Programming
– view computer programs as collection of
objects.
– Objects have attributes and actions.
C++ Course SarMag Trimester 3
14
As a C/C++ program
set memory[801] to hold 00000001
x=1;
set memory[802] to hold 00000000
i=0;
if memory[802] = 10 jump to instruction #8
while (i!=10) {
increment memory[802]
}
i++;
set memory[803] to 2 times memory[801]
put memory[803] in to memory[801]
jump to instruction #3
print memory[801]
x=x*2;
}
printf("%d",x);
C++ Course SarMag Trimester 3
15
Compiler
C++ Program
C++ Compiler
int main() {
int i=1;
. . .
Machine
Language
Program
01001001
10010100
Created with text editor or
development environment
C++ Course SarMag Trimester 3
16
Many Different Compilers
• There are many different C++ Compilers:
–
–
–
–
–
Microsoft Visual C++
Borland C++
GNU g++
IBM xlc
Sun CC
C++ Course SarMag Trimester 3
17
What is an Algorithm?
• Algorithm = Calculation Procedure
• The traditional definition of "algorithm" is that it
is a formally defined procedure for performing
some calculation
• A finite ordered set of well defined rules for the
solution of a problem. Algorithms are often
written in pseudocode, so that they can be easily
converted in many different programming
languages
C++ Course SarMag Trimester 3
18
What is an Algorithm?
• algorithm is a set of step-by-step
commands or instructions designed to reach
a particular goal
• algorithm
A mathematical procedure used to solve
problems with a series of steps.
C++ Course SarMag Trimester 3
19
Properties of Algorithms
•
•
•
•
•
•
1. Finiteness/ Terminate
2. Absence of Ambiguity
3. Definite/ Clear
4. Efective
5. Input
6. Output
(KNU-69, HOR-90)
C++ Course SarMag Trimester 3
20
Prinsip Pemrograman
• Harus dapat menentukan masalah
sesungguhnya.
• Design program
• Struktur data
• Coding program
• Testing program
• Pemeliharaan program + Dokumentasi
C++ Course SarMag Trimester 3
21
Aturan pemrograman
• Buat dokumentasi
• Buat semudah mungkin
• Gunakan modul jika dirasa program cukup
besar
• Gunakan fungsi untuk suatu hal yg sering di
gunakan
• Hindari penggunaan var global
C++ Course SarMag Trimester 3
22
Descargar

Background - Gunadarma University