Chapter 11 Getting ready to program Hardware Model Software Model Programming Languages Facts...

26
Chapter 1 1 Chapter 1 Getting ready to program Hardware Model Software Model Programming Languages Facts about C++ Program Development Process The Hello-world Program

description

Chapter 13 Program & Data  Program - a sequence of instructions specifying how the data is to be processed.  Data - input to the program, either supplied during runtime or pre-stored in the computer  Both program and pre-stored data are stored in the main memory

Transcript of Chapter 11 Getting ready to program Hardware Model Software Model Programming Languages Facts...

Page 1: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 1

Chapter 1 Getting ready to program

Hardware Model Software Model Programming Languages Facts about C++ Program Development Process The Hello-world Program

Page 2: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 2

Stored-Program Computer Both programs and data are stored in main

memory

Secondary Storage

Input Devices

Output Devices

CPU

Main Memory

Page 3: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 3

Program & Data Program - a sequence of instructions specifying

how the data is to be processed. Data - input to the program, either supplied during

runtime or pre-stored in the computer Both program and pre-stored data are stored in the

main memory

Page 4: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 4

Main Memory The place for storing data of all kinds Bit (Binary Digit, smallest unit) - stores either 0 or 1 Byte - consists of 8 bits Main memory

a list of bytes, each associated with an address stores both the program and data e.g. 64K bytes of memory:

0

1

2

65535

.

.

.

.

.

.

Page 5: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 5

CPU Acronym for … The “brain” of computer Consists of a processing unit (the ALU), a control

unit (the CU) and a set of registers (a few high-speed memory units for temporary use)

Fetch and execute instructions from the main memory until the computer is turned off

ALU

CU

Register FileCPU

Page 6: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 6

Software a collection of programs for a specific task e.g. operating system (OS), editor, compiler,

game, database server

Operating System Chief servant, managing tasks and resources allocates the computer’s resources to the different

tasks that the computer must accomplish What are the most popular OS’s?

e.g. DOS, Windows, UNIX, Solaris, Linux, etc.

Page 7: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 7

EditorPascal

Compiler C++ Compiler

Games

Web Browser

System and Application Softwares

Hardware-Software Hierarchy

Operating System

Hardware

Page 8: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 8

Hardware-Software Hierarchy

Page 9: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 9

Machine Languages A CPU only understands its own machine

language => portability problem In Motorola 68000, the following 4 bytes is an

instruction to move the value in register D3 to memory address 104.

In Intel 80486, the same sequence of bytes represents different instructions

Coding is tedious and error prone

192

49

104

0

Page 10: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 10

Assembly Languages Use English-like abbreviations

e.g. the previous MC68000 machine instruction is written as:

MOVE D3, 104 slightly easier for human to understand need an assembler to translate into machine instructions Different CPUs have different instruction sets

e.g. MOVE D3, 104 is not a valid instruction in Intel 80486 since it doesn’t have register D3

=> portability problem

Page 11: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 11

High-level Languages Close(r) to human language One single statement accomplishes substantial

tasks Need a compiler/linker to translate into machine

language More portable - the same program (more or less)

works for different machines e.g. Fortran, COBOL, Pascal, Ada, Modula, C, C+

+, Lisp, Prolog, Java, Perl...

Page 12: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 12

Compiler & Linker Compiler

translates a program in high-level language into an object program (or object code). The original program is called the source program/code.

Linker combines the object code of a

program with other pre-compiled object codes to generate an executable code which is in machine language

linker

compiler

sourcecode

objectcode

other objectcode

executablecode

Page 13: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 13

The C++ Program Language C

developed by Dennis Ritchie in 1970’s originally for writing system programs such as OS

(e.g. UNIX) and compilers “close to machine”

C++ developed by Bjarne Stroustrup in 1980’s C enhanced with object-oriented features, for

writing more complex programs “close to the problems to be solved”

Page 14: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 14

Standardization & Libraries ANSI/ISO Standard for C++:

An important standard, most compilers followDocuments available in their web:

webstore.ansi.org(Document No.: INCITS/ISO/IEC 14882-2003)

C++ Standard Libraries Contains pre-written classes, objects & functions Greatly facilitates programming and performance Generally provided by compiler vendors

Proprietary libraries provided by individual vendors

Page 15: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 15

Program Development Process (1) Specify the task Find an algorithm for its solution Code the algorithm in a programming language

Prepare the source code (program) in a file, called source file

Test the code Compile the source file to produce object code, stored

in an object file Link up the object code with any library module and

load the loader to produce the final executable file Run the executable file and debug any error

Page 16: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 16

Program Development Process (2)

Problemdefinition

Algorithmdesign

Translatingto C++

Testing

Page 17: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 17

Programming Environment (1)

Editor Disk

Preprocessor Disk

Compiler Disk

Linker Disk

Page 18: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 18

Programming Environment (2)

.

.

.

.

.

.

Loader

CPU

Disk

Main Memory

Main Memory

Page 19: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 19

Programming Errors Syntax errors:

violation of the syntax detected during compilation

Run-time errors: compilation successful but errors detected during

run-time, e.g. division by zero Logic error:

compilation and execution do not produce machine detectable errors but the answer is wrong

may be due to wrong algorithm or wrong coding

Page 20: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 20

Your first C++ Program#include <iostream>using namespace std;

void main(){cout << "Hello, world!“ << endl;

}

include library <iostream>

use std namespace

output statement

Page 21: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 21

Components of the Program “#include…” - called include directives, tells the

compiler to include the header of the library <iostream>

The line “using namespace std;” tells the compiler to include the std namespace

The lines “int main(){ ” and “return 0; }” tell the compiler where the main body of program starts and ends

The line “cout << …” is an executable statement (or simply statement). It causes “Hello, world!” to be printed on the screen.

Page 22: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 22

Library Library is a collection of classes, objects and

functions (see p14, more detail in Chapter 4, 5…) We can use the pre-written classes, objects and

functions inside a library after including it using “#include…”

For example, cout and cin are objects inside the iostream library. So, we can use them after having the directive: “#include <iostream>”

Page 23: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 23

Namespace// without “using namespace std;”#include <iostream>void main (){ std::cout << "Hello world” << endl; }

// with “using namespace std;”#include <iostream>using namespace std;void main () { cout << "Hello world“ << endl;}

Page 24: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 24

Further Details C++ is case-sensitive:

E.g. “Main” is different from “main”. Syntax of include directives:

no space after < and before > no semi-colon at the end, each include directive

must be on its own line Statements are ended with semicolons

spacing is not important programmer can put in suitable spacing and

indentation to increase readability

Page 25: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 25

Output statement: << - the output (or insertion) operator cout << “Hello…” - apply the output operator

on the objects cout and “Hello…” The object cout is called standard output stream

and is defined in <iostream> endl – causes the cursor to move to the beginning of

a new line

Page 26: Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.

Chapter 1 26

Layout of a Simple C++ Program#include <iostream>using namespace std;

void main(){statement-1statement-2

statement-3 ... statement-last}