Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process...

42
Chapter 9 High-Level Programming Languages: C++

Transcript of Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process...

Page 1: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Chapter 9

High-Level Programming Languages: C++

Page 2: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

2

Chapter Goals

• Describe the translation process and distinguish between assembly, compilation, interpretation, and execution

• Name four distinct programming paradigms and name a language characteristic of each

• Define the concepts of a data type and strong typing

• Describe the structure of a C++ program

Page 3: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Chapter Goals

• Construct valid numeric and string expressions

• Construct input and output expressions

• Implement selection statements in C++

• Implement loop structures in C++

• Implement void and value-return functions

• Define subprogram statements and their use

• Use of nested logic

3

Page 4: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

4

Compilers

• High-level languages provide a richer set of instructions that makes the programmer’s life even easier– C++, Java, Lisp, Prolog are high-level

languages

• Compiler A program that translates a high-level language program into machine code– C++ is a compiled language

Page 5: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

5

Compilers

Figure 8.1 Compilation process

Page 6: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Interpreters

• Interpreter An interpreter translates a statement and then immediately executes the statement

– Lisp and Prolog are interpreted languages

• Java is first compiled in an intermediate language (called Bytecode) and the Bytecode is interpreted.

6

Page 7: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

7

Java

• Introduced in 1996 and swept the computing community by storm

• Java is compiled into a standard machine language called Bytecode

• A software interpreter called the JVM (Java Virtual Machine) takes the Bytecode program and executes it

• Portability was of primary importance

Page 8: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

8-8

C++ Portability

Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode

Page 9: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

8-9

Java Portability

Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode

Page 10: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

10

Programming Language Paradigms

• What is a paradigm?

• A set of assumptions, concepts, values, and practices that constitute a way of viewing reality

Page 11: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

11

Programming Language Paradigms

• Imperative or procedural model– FORTRAN, COBOL, BASIC, C, Pascal, Ada

• Functional model– LISP, Scheme (a derivative of LISP), and ML

Page 12: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

8-12

Programming Language Paradigms

• Logic programming– PROLOG

• Object-oriented paradigm– SIMULA and Smalltalk– C++ is as an imperative language with some

object-oriented features– Java is an object-oriented language with

some imperative features

Page 13: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

A simple C++ program

13

Page 14: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

A C++ program template

14

Page 15: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Introduction to C++

• Some components of a C++ template program

– Comments

• Give information to human readers of code

– Include directive

• The linker includes object code from a library

– Using directive

• Tells compiler to look in a namespace for definitions not mentioned in the program

15

Page 16: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Virtual Data Storage

• Identifiers: names in a programming language

• Keywords: have special meanings in C++

• C++: case-sensitive, free-format language

• Data items can be constants or variables

16

Page 17: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Some C++ Standard Data Types

17

• A declaration of a data item tells– Whether the item is a constant or a variable– The identifier used to name the item– The data type of the item

Example of a variable integer declarationint numb;

Page 18: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

18

Other Data Types

• An array– Groups together a collection of memory locations, all storing data of the same type

Example:int Hits[12];

Page 19: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Other data types

• Strings– A string is a sequence of characters

Example:string s=“Intro to CS”;

creates a string s that contains “Intro to CS”

19

Page 20: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

20

Statement Types

• Input/output statement

– Input statement

• Collects a specific value from the user for a variable within the program

– Output statement

• Writes a message or the value of a program variable to the user’s screen or to a file

Page 21: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

21

Control Statements

• Types of control mechanisms

– Sequential

• Instructions are executed in order

– Selection or Conditional

• Choice of which instructions to execute next depends on some condition

– Looping

• Group of instructions may be executed many times

Page 22: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

22

Input Statements

• Example– Pseudocode

Get value for Radius

– C++cin >> Radius;

• cin: input stream

• Code for extraction operator (>>) and the definition of the cin stream come from the iostream library and std namespace

Page 23: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

23

Output Statements

• Example– Pseudocode

Print the value of Circumference

– C++ cout << Circumference;

• cout: output stream

• Code for the insertion operator (<<) and the definition of the cout stream come from the iostream library and std namespace

Page 24: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Input and Output

• Cin reads from the keyboard and cout writes on the screen– Cin skips the whitespace

• Function get reads one character from an input stream– Stores the character read in a variable of type

char, the single argument the function takes

cin.get(character);

24

Page 25: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

The Assignment Statement

• General form– Pseudocode

Set the value of “variable” to “arithmetic expression”

– C++variable = expression;

1. Expression on the right is evaluated

2. The result is written into the memory location named on the left

25

Page 26: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Selection Statements

• Default mode of execution: sequential

• Selection statement

• Evaluation of a Boolean condition (also called a Boolean expression)

• Which programming statement to execute next is decided based on the value of the Boolean condition (true or false)

26

Page 27: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Selection Statements

• Selection statements– if-else statement

if (Boolean condition)

S1;

else

S2;

– if variation of the if-else statement

if (Boolean condition)

S1;

27

Page 28: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Loops

• Looping (iteration)

– The loop body may be executed repeatedly based on the value of the Boolean condition

– while statement

while (Boolean condition)

S1;

28

Page 29: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Putting the Pieces Together

• At this point, we can:

– Perform input and output

– Assign values to variables

– Direct the flow of control using conditional statements or looping

• For a complete program, we need to:

– Assemble the statements in the correct order

– Fill in the missing pieces

29

Page 30: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Simple example in C++Problem: Write a program that evaluates the arithmetic

expression A = B + C – 7. Assign an arbitrary value to B and C inside the program. Print A.

• This corresponds to the pseudo code instruction:• Set A to B plus C minus 7• Print A

• The C++ program:

30

//simple example#include <iostream>using namespace std;

int main(){

int B=10;int C=5;int A=B+C-7;cout << “the value of A is “ <<

A << endl;return 0;}

Page 31: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Modification 1 of the simple example in C++

• MODIFICATION 1: Now I want to ask the user to provide for me the values of B, and C. Then I want to output the value of A.

– This corresponds to the pseudo code:– Get the value of B and C – Print the value of A

• cin and cout are the commands for getting data in input from the keyboard and printing a data in output on the screen

• Note that in computer numbers are converted in characters in order to be printed since the screen displays ASCII codes!

31

Page 32: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Simple example in C++: Modification 1

• The C++ program:

32

//simple example: Modification 1#include <iostream>using namespace std;

int main(){

int B, C;cout << “Insert the value of B: “;cin >> B;cout << “Insert the value of C: “;cin >> C;int A=B+C-7;cout << “the value of A is “ << A

<< endl;return 0;}

Page 33: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Modification 2 of the simple example in C++

• MODIFICATION 2: The second change consists in checking if B and C are positive (negative values are not accepted). If B and C are not positive, force them to be positive (i.e. If B receive -5 turn B into +5). Then set A=B+C-7

–This corresponds to the pseudo code:

If (B < 0) then Set B to B*(-1)

If (C < 0) then Set C to C*(-1)

Set A to B+C-7

33

Page 34: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Simple example in C++: Modification 2

• The new C++ program:

34

//simple example: Modification 2#include <iostream>using namespace std;

int main(){

int B, C;cout << “Insert the value of B: “;cin >> B;if (B < 0) B=B*(-1);cout << “Insert the value of C: “;cin >> C; if (C < 0) C=C*(-1);int A=B+C-7;cout << “the value of A is “ << A

<< endl;return 0;}

Use of IF

Page 35: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Modification 3 of the simple example in C++

• Nested Logic

• MODIFICATION 3: Repeat the previous program 10 times and each time get new values in input for B and C.

–This corresponds to the pseudo code:

Set count to 0while (count < 10)

execute the programIncrement count

endwhile

35

IF within a WHILE

Page 36: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Simple example in C++: Modification

3

• The new C++ program:

36

//simple example: Modification 3#include <iostream>using namespace std;

int main(){ int B, C; int count=0; while (count <10) { cout << “Insert the value of B: “;

cin >> B;if (B < 0) B=B*(-1);cout << “Insert the value of C: “;cin >> C; if (C < 0) C=C*(-1);int A=B+C-7;cout << “the value of A is “ << A

<< endl;count ++;

}return 0;}

IF within a WHILE

Page 37: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Modification 4 of the simple example in C++

• MODIFICATION 4: Instead of repeating 10 times I want the user to decide if the execution of the program must be repeated or not. I will ask the user to insert a 0 to stop the program. Any other value will force the program to be re-executed.

–This corresponds to the pseudo code:

Set the answer to “yes”;While (answer is “yes”) repeat the program Ask the user if he/she wants to repeat

Get the answerendwhile

37

Page 38: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Simple example in C++: Modification

4

• The new C++ program:

38

//simple example: Modification 4#include <iostream>using namespace std;

int main(){ int B, C; char answer=‘y’; while ((answer == ‘y’) || (answer ==‘Y’)) { cout << “Insert the value of B: “;

cin >> B;if (B < 0) B=B*(-1);cout << “Insert the value of C: “;cin >> C; if (C < 0) C=C*(-1);int A=B+C-7;cout << “the value of A is “ << A

<< endl;cout << “Do you want to repeat this

program? (y/n) “ << endl;cin >> answer;

}return 0;}

Page 39: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

A new problem in C++

• Problem

– Read in a sequence of non-negative numbers, one number at a time, and compute a running sum.

– When you encounter the first negative number print out the sum of all the non-negative values received and stop.

39

This Exercise is for you to complete!

Page 40: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

The pseudocode

40

Page 41: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

41

Subprogram Statements

We can give a section of code a name and use that name as a statement in another part of the program

When the name is encountered, the processing in the other part of the program halts while the named code is executed

It is implemented by using functions.

Remember?

Page 42: Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Simple example in C++: Modification

5

• Break the problem in smaller parts – use subprograms

• The new C++ program:

42

//simple example: Modification 5#include <iostream>using namespace std;

int check(int x); // function for subprogram{if (x < 0) x=x*(-1);}

int main(){ int B, C; char answer=‘y’; while ((answer == ‘y’) || (answer ==‘Y’)) { cout << “Insert the value of B: “;

cin >> B;B=check(B);cout << “Insert the value of C: “;cin >> C;C=check(C); int A=B+C-7;cout << “the value of A is “ << A

<< endl;cout << “Do you want to repeat this

program? (y/n) “ << endl;cin >> answer;

}return 0;}

Functions