38 4/11/98 CSE 143 Modules [Chapter 2]. 39 4/11/98 What is a Module?  Collection of related items packaged together  Examples:  Stereo System Components.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
The Software Lifecycle. Example Problem: Update a Checkbook Write a program that allows the user to enter a starting balance, a transaction type, D or.
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Software Engineering and Design Principles Chapter 1.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Design The goal is to design a modular solution, using the techniques of: Decomposition Abstraction Encapsulation In Object Oriented Programming this is.
CS 117 Spring 2002 Functions Hanly - Chapter 5 Friedman-Koffman - Sections & Chapter 6.
CS 201 Functions Debzani Deb.
Guide To UNIX Using Linux Third Edition
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
Chapter 4 Procedural Abstraction and Functions That Return a Value.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
UNIT 13 Separate Compilation.
CS 213 Fall 1998 Namespaces Inline functions Preprocessing Compiling Name mangling Linking.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
1 Data Structures and Algorithms Week 3 Data Abstraction Part I: Modules and Information Hiding.
Introduction to Classes and Objects. Real Life When a design engineer needs an electrical motor he doesn’t need to worry about –How a foundry will cast.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
FALL 2001ICOM Lecture 01 ICOM 4015 Advanced Programming Lecture 0 Review of Programming I Reading: LNN Chapters 1-3,5-6.
1 Review: C++ class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions Procedural Abstraction Flow of Control INFSY 307 Spring 2003 Lecture 4.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
What Is? function predefined, programmer-defined
Separate Compilation and Namespaces
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Chapter Structured Types, Data Abstraction and Classes
CSC113: Computer Programming (Theory = 03, Lab = 01)
CS1010 Programming Methodology
CS1010 Programming Methodology
Lecture 1 Introduction.
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
Separate Compilation and Namespaces
User Defined Functions
Classes and Data Abstraction
C++ Compilation Model C++ is a compiled language
Programs written in C and C++ can run on many different computers
Predefined Functions Revisited
Review: C++ class represents an ADT
What Is? function predefined, programmer-defined
CPS120: Introduction to Computer Science
Presentation transcript:

38 4/11/98 CSE 143 Modules [Chapter 2]

39 4/11/98 What is a Module?  Collection of related items packaged together  Examples:  Stereo System Components  Computer Components  Application Suites

40 4/11/98 Example Turntable Tuner CD Player Tape Deck  Two ways to design a stereo system Turntable Tuner CD Player Tape Deck All-in-one design Modular design What are the advantages of each?

41 4/11/98 Modularization  Basic idea: break apart large system into smaller units (modules)  Group related functionality in one module  Often continued in a hierarchy of modules  Design modules to be general and reusable  Multiple times in same program  Different programs/programmers

42 4/11/98 Specification vs. Implementation Two parts of each module  Specification (what)  Also known as “interface”  Describes what services module provides to clients (users)  Publicly visible  Implementation (how)  Parts of the module that actually do work  Private, hidden behind module interface

43 4/11/98 Specification as Contract Module specification acts as a contract between client and implementor  Client depends on specification not changing  Doesn’t need to know any details of how module works, just what it does  Implementor can change anything not in the specification, (eg. to improve performance)  Implementation is a “black box” (encapsulation), providing information hiding

44 4/11/98 Locality  Locality of design decisions from encapsulation  Benefits of private data and algorithm locality:  Division of labor  Easier to understand  Implementation independence

45 4/11/98 Modules in C++  Modules represented by a pair of files  specification (.h ) file  implementation (.cpp,.cc,.c++,.C, etc) file  Client’s only interaction with module is through the interface defined in the.h file

46 4/11/98 Imports and Exports  Specification (. h ) file declares which items are exported  constants, function prototypes, and data types  Client program must import features of a module to use them  Use the #include directive  Implementation (. cpp ) file also uses #include to ensure it obeys the contract

47 4/11/98 Specification  Supplies constants, data types, function prototypes  Comments describing what each function does  Preconditions: What should be true before function call (e.g., relationships among arguments)  Postconditions: What is true after function return (e.g., relationship of return value to arguments)  Invariants: What must be true while function executes (e.g., relationships among local variables)

48 4/11/98 Sample Specification File // Specification file for computational geometry // functions // POST: returns the area of a circle with given // radius double circleArea (double radius); // PRE: area must be non-negative // POST: returns the radius of a circle of given // area double circleRadius(double area); geometry.h prototype

49 4/11/98 Sample Implementation File // Implementation of geometry functions #include "geometry.h" #include const double PI = ; double circleArea (double radius) { return PI * radius * radius; } double circleRadius (double area) { return sqrt(area/PI); } geometry.cpp

50 4/11/98 Sample Client File #include #include "geometry.h" int main(void) { double value; cout << "Enter radius: "; cin >> value; cout << "Area of circle is " << circleArea(value) << endl; return 0; } main.cpp

51 4/11/98 Building the Program Three stages to go from source code to executable:  Preprocess  reads #include files, expands #define  Compile  Converts C++ code to object code the computer can understand  Link  Connects your object code with system libraries to make an executable program

52 4/11/98 Building the Program (2) Compile compiler main.cpp geometry.h Compile compiler iostream.h Link linker main.exe geometry.obj main.obj geometry.cpp geometry.h math.h geometry.obj main.obj libraries

53 4/11/98 Separate Compilation  Each module’s.cpp source code is converted into object code separately  Linker collects object code together to build executable  Many environments hide this process from you  On MSVC, just press the “build all” button (or even just “run” …)  Must be done “manually” under UNIX

54 4/11/98 Advantages of Separate Compilation  One module usable by many clients  Individual modules may be changed and recompiled without changing entire program  Client’s code can be changed and recompiled without recompiling modules  Can distribute object code to protect secrets  Interface (specification) changes mandate recompiling both implementation and client

55 4/11/98 Designing Modules  Must think about implementor’s and client’s roles  Implementor’s goals:  Provide a reusable, robust abstractions  Make interface independent of client  Keep freedom to modify implementation  Protect module from abuse and misuse  Client’s goals:  Assemble a program from usable modules  Rely solely on specification  Very hard to do well!

56 4/11/98 Standard Libraries  C/C++ comes with some predefined modules (libraries)  iostream.h, fstream.h for stream I/O  math.h for sin, cos, sqrt, etc.  string.h for strcmp, strlen, etc.  Compilers also include nonstandard libraries  Graphics, windowing, etc.  Please don’t use them for CSE 143

57 4/11/98 Compile-time error if identifiers (function names, constants, etc.) are defined multiple times: Multiple Inclusion... const int MINSIZE = 20; void writeLetters (ofstream& otfile);... #include "lmatrix.h"... #include "lmatrix.h" #include "word.h"... word.h lmatrix.h main.cpp

58 4/11/98 Multiple Inclusion (2)  To avoid this problem, use preprocessor directives: // lmatrix.h #ifndef _LMATRIX_H_ #define _LMATRIX_H_... const int MINSIZE = 20; void writeLetters (ofstream& otfile);... #endif  Read the above as: If _LMATRIX_H_ undefined, compile the code through #endif Preprocessor directive

59 4/11/98 Summary  Specification (.h ) and implementation (.cpp ) files  Specification as contract  Information hiding, black box analogy, locality  Imports and exports, public and private code  Separate compilation