Chapter 8 Introduction to HighLevel Language Programming Invitation


















































- Slides: 50

Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, C++ Version, Fourth Edition

Objectives In this chapter, you will learn about n Where do we stand? n High-level languages n Introduction to C++ n Virtual data storage n Statement types Invitation to Computer Science, C++ Version, Fourth Edition 2

Objectives (continued) n Putting the pieces together n Managing complexity n Object-oriented programming n Graphical programming n The big picture: Software engineering Invitation to Computer Science, C++ Version, Fourth Edition 3

Where Do We Stand? n Early days of computing q Programmers used assembly language n n Programs written by technically oriented people Later decades q Programmers demanded a more comfortable programming environment n Programs could be written by “nontechie” people Invitation to Computer Science, C++ Version, Fourth Edition 4

High-level Languages n High-level programming languages q Called third-generation languages q Overcame deficiencies of assembly language q Programmer didn’t need to manage details of data storage or movement Invitation to Computer Science, C++ Version, Fourth Edition 5

High-level Languages (continued) n Expectations of a high-level language program (continued) q q q Programmer can take a macroscopic view of tasks; “primitive operations” can be larger Program will be portable Code will be closer to standard English and use standard mathematical notation Invitation to Computer Science, C++ Version, Fourth Edition 6

Figure 8. 1 Transitions of a High-level Language Program Invitation to Computer Science, C++ Version, Fourth Edition 7

Figure 8. 2 A Simple C++ Program Invitation to Computer Science, C++ Version, Fourth Edition 8

Introduction to C++ n Some components of program in Figure 8. 2 q Comments n q Include directive n q Give information to human readers of code The linker includes object code from a library Using directive n Tells compiler to look in a namespace for definitions not mentioned in the program Invitation to Computer Science, C++ Version, Fourth Edition 9

Figure 8. 3 The Overall Form of a Typical C++ Program Invitation to Computer Science, C++ Version, Fourth Edition 10

Virtual Data Storage n Identifiers: Names in a programming language n Keywords: Have special meanings in C++ is a case-sensitive, free-format language n Data items can be constants or variables Invitation to Computer Science, C++ Version, Fourth Edition 11

Virtual Data Storage (continued) n A declaration of a data item tells q Whether the item is a constant or a variable q The identifier used to name the item q The data type of the item Invitation to Computer Science, C++ Version, Fourth Edition 12

Figure 8. 5 Some of the C++ Standard Data Types Invitation to Computer Science, C++ Version, Fourth Edition 13

Virtual Data Storage (continued) n An array q Groups together a collection of memory locations, all storing data of the same type Figure 8. 6 A 12 -Element Array Hits Invitation to Computer Science, C++ Version, Fourth Edition 14

Statement Types n Input/output statements q Input statement n q Collects a specific value from the user for a variable within the program Output statement n Writes a message or the value of a program variable to the user’s screen or to a file Invitation to Computer Science, C++ Version, Fourth Edition 15

Statement Types (continued) n Assignment statement q n Assigns a value to a program variable Control statement q Directs the flow of control n Can cause it to deviate from usual sequential flow Invitation to Computer Science, C++ Version, Fourth Edition 16

Input/Output Statements n Example q Pseudocode Get value for Radius q C++ cin >> Radius; n n cin: Input stream Code for extraction operator (>>) and the definition of the cin stream come from the iostream library and std namespace Invitation to Computer Science, C++ Version, Fourth Edition 17

Input/Output Statements (continued) n Example q Pseudocode Print the value of Circumference q C++ cout << Circumference; n n cout: Output stream Code for the insertion operator (<<) and the definition of the cout stream come from the iostream library and std namespace Invitation to Computer Science, C++ Version, Fourth Edition 18

The Assignment Statement General form n q Pseudocode Set the value of “variable” to “arithmetic expression” q C++ variable = expression; 1. 2. Expression on the right is evaluated The result is written into the memory location specified on the left Invitation to Computer Science, C++ Version, Fourth Edition 19

Control Statements n Types of control mechanisms q Sequential n q Conditional n q Instructions are executed in order Choice of which instructions to execute next depends on some condition Looping n Group of instructions may be executed many times Invitation to Computer Science, C++ Version, Fourth Edition 20

Control Statements (continued) n Default mode of execution: sequential n Conditional flow of control q q Evaluation of a Boolean condition (also called a Boolean expression) Which programming statement to execute next is based on the value of the Boolean condition (true or false) Invitation to Computer Science, C++ Version, Fourth Edition 21

Control Statements (continued) n Conditional flow of control (continued) q if-else statement if (Boolean condition) S 1; else S 2; q if variation of the if-else statement if (Boolean condition) S 1; Invitation to Computer Science, C++ Version, Fourth Edition 22

Figure 8. 10 Conditional Flow of Control (If-Else) Invitation to Computer Science, C++ Version, Fourth Edition 23

Figure 8. 11 If-Else with Empty Else Invitation to Computer Science, C++ Version, Fourth Edition 24

Control Statements (continued) n Looping (iteration) q q The loop body may be executed repeatedly based on the value of the Boolean condition while statement while (Boolean condition) S 1; Invitation to Computer Science, C++ Version, Fourth Edition 25

Figure 8. 12 While Loop Invitation to Computer Science, C++ Version, Fourth Edition 26

Putting the Pieces Together n At this point, we can q Perform input and output q Assign values to variables q n Direct the flow of control using conditional statements or looping For a complete program, we need to q Assemble the statements in the correct order q Fill in the missing pieces Invitation to Computer Science, C++ Version, Fourth Edition 27

Meeting Expectations n C++ meets the four expectations for a high-level programming language n Expectations q A programmer need not manage the details of the movement of data items within memory, nor pay any attention to where they are stored Invitation to Computer Science, C++ Version, Fourth Edition 28

Meeting Expectations (continued) n Expectations (continued) q q q Programmer can take a macroscopic view of tasks, thinking at a higher level of problem solving Programs written in high-level languages will be portable rather than machine-specific Programming statements in a high-level language n Will be closer to standard English n Will use standard mathematical notation Invitation to Computer Science, C++ Version, Fourth Edition 29

Managing Complexity: Divide and Conquer n Divide and conquer q n To solve a problem, divide it into smaller pieces In a computer program q q Divide the code into modules (subprograms or functions in C++), each doing a part of the overall task Empower these modules to work together to solve the original problem Invitation to Computer Science, C++ Version, Fourth Edition 30

Figure 8. 17 A Structure Chart Figure 8. 18 A More Detailed Structure Chart Invitation to Computer Science, C++ Version, Fourth Edition 31

Using Functions n Function q A module of code in C++ q Named using ordinary C++ identifiers n Subtask functions: Optional n The main function: Mandatory Invitation to Computer Science, C++ Version, Fourth Edition 32

Using Functions (continued) n To invoke a subtask function, the main function gives q Name of the function q Argument list for the function n Argument list: List of identifiers for variables that concern that function n Any function can have its own constant and variable declarations Invitation to Computer Science, C++ Version, Fourth Edition 33

Writing Functions n A function header consists of: q n Return indicator: Classifies a function as a void or a nonvoid function q Function identifier q Parameter list By default, arguments in C++ are passed by value Invitation to Computer Science, C++ Version, Fourth Edition 34

Figure 8. 22 The Outline for a C++ Function Invitation to Computer Science, C++ Version, Fourth Edition 35

Figure 8. 27 Some C++ Terminology Invitation to Computer Science, C++ Version, Fourth Edition 36

Object-Oriented Programming n Object-oriented programming (OOP) q q n A program is a simulation of some part of the world that is the domain of interest Each object is an example drawn from a class of similar objects Key elements of OOP q Encapsulation n n A class consists of its subtask modules and its properties Both are “encapsulated” in the class Invitation to Computer Science, C++ Version, Fourth Edition 37

Object-Oriented Programming (continued) n Key elements of OOP (continued) q Inheritance n q Once a class A of objects is defined, a class B of objects can be defined as a “subclass” of A Polymorphism n One name, the name of the service to be performed, has several meanings, depending on the class of the object providing the service Invitation to Computer Science, C++ Version, Fourth Edition 38

What Have We Gained? n Two major advantages of OOP q Software reuse q A more natural “world view” Invitation to Computer Science, C++ Version, Fourth Edition 39

Graphical Programming: Graphics Primitives n Bitmapped display q n Frame buffer q n The screen is made up of thousands of pixels, laid out in a two-dimensional grid Memory that stores the actual screen image The terminal hardware displays on the screen the frame buffer value of each pixel Invitation to Computer Science, C++ Version, Fourth Edition 40

Figure 8. 32 Pixel Numbering System in a Bitmapped Display Invitation to Computer Science, C++ Version, Fourth Edition 41

Graphics Primitives (continued) n Graphics library q n Software containing a collection of functions that control the setting and clearing of pixels Virtually all modern programming languages come with a graphics library Invitation to Computer Science, C++ Version, Fourth Edition 42

The Big Picture: Software Engineering n Software life cycle q q Overall sequence of steps needed to complete a large-scale software project Implementation represents a relatively small part of the cycle Invitation to Computer Science, C++ Version, Fourth Edition 43

Figure 8. 35 Steps in the Software Development Life Cycle Invitation to Computer Science, C++ Version, Fourth Edition 44

Scaling Up n Programs written by students q n Real-world programs q n No longer than a few hundred lines 2, 3, or 4 orders of magnitude larger Large-scale software development q Extensive planning and design needed q A team of programmers needed q Software engineering Invitation to Computer Science, C++ Version, Fourth Edition 45

The Software Life Cycle n Each step in the software development life cycle q Has a specific purpose and activities q Should result in a written document n The feasibility study n Problem specification n Program design Invitation to Computer Science, C++ Version, Fourth Edition 46

The Software Life Cycle (continued) n Algorithm selection or development, and analysis n Coding n Debugging n Testing, verification, and benchmarking n Documentation n Maintenance Invitation to Computer Science, C++ Version, Fourth Edition 47

Modern Environments n Integrated Development Environment (IDE) speeds development by providing q A text editor q A file manager q A compiler q A linker and loader q Tools for debugging Invitation to Computer Science, C++ Version, Fourth Edition 48

Summary n In a high-level language, the programmer q Need not manage storage q Can think about the problem at a higher level q q Can use more powerful program instructions that are more like natural language Can write a much more portable program Invitation to Computer Science, C++ Version, Fourth Edition 49

Summary (continued) n C++ is an object-oriented, high-level programming language n if-else statement creates a conditional flow of control n while loop can be used for iteration n Software life cycle: Overall sequence of steps needed to complete a large-scale project Invitation to Computer Science, C++ Version, Fourth Edition 50