12
CSci 162 Lecture 8 Martin van Bommel

CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Embed Size (px)

DESCRIPTION

Procedural Abstraction After breaking down problem into solvable chunks, must think about chunks separately Procedural abstraction –separate concerns of what a function does from details of how it does it Permits use of function in design of problem solution before implementation is done Think - integer array functions

Citation preview

Page 1: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

CSci 162

Lecture 8

Martin van Bommel

Page 2: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Large-scale Programming• Up to now we have been writing relatively short

programs to solve simple problem• Want to now think about the management of

large-scale programs• Top-down design still works to modularize a large

project into individual pieces• Want pieces to be implemented by different

programmers

Page 3: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Procedural Abstraction• After breaking down problem into solvable chunks,

must think about chunks separately• Procedural abstraction

– separate concerns of what a function does from details of how it does it

• Permits use of function in design of problem solution before implementation is done

• Think - integer array functions

Page 4: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Data Abstraction• Data abstraction

– separate concerns of types of data objects and the associated operations from the details of how represented in memory and implemented

• Permits focus on the use of the data in solving larger problems

• Again think integer array and its operations

Page 5: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Information Hiding• Abstractions enable designer to make

implementation decisions in a more ad-hoc fashion - can postpone implementation

• Concealing details of implementation of data object and only enabling manipulation via defined operations allows changes to be made at lower level without affecting upper

• Protecting implementation details from user is called information hiding

Page 6: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Reusable Code• Reusable code - can be used by many different

applications• Encapsulation - packaging data object together

with its operators in a library• Use #include to access the library• Want to be able to provide these ourselves

Page 7: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Header Files• Header file - text file containing all information

about a library needed by the compiler and the user of the library

• Contents– comment summarizing library’s purpose– #define directives– type definitions for library– comments for each library function– function prototypes in form extern prototype

Page 8: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Implementation Files• Implementation file – C++ source file containing

code of library’s functions and any other information needed for compiling

• Contents– comment summarizing library’s purpose– #include directives for header file and others– #define directives needed in implementation– type definitions used only in implementation– function implementations (including comments)

Page 9: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Interface

• Conceptual entity• Boundary between the implementation of a library

and programs that use that library• Information passes across boundary whenever

functions in library are called• Interface mediates and gives structure to the

exchange of information between library and its users - .h file serves as interface

Page 10: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Example

• Math library - sqrt function• Implementation contains steps to calculate square

root of a number• User needs not know implementation, only use of

function – - how to pass in value and get back result

• Shown in <cmath> - file “cmath.h”– double sqrt(double);

Page 11: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Interface Terminology

• Implementor - defines functions– programmer who implements a library

• Client - uses functions– programmer who calls functions in library

• Both must know:– Function name– The arguments and their types– The type of the return result

Page 12: CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to

Interface Design

• Unified - single abstraction with theme• Simple - hide all complexity from client• Sufficient - meet the needs of client• General - meet needs of different clients• Stable - never change structure and effect

of a function in interface, even ifimplementation changes or morefunctions added