I. Modules (HR 2) A. what modules are (HR 2.1, HR 2.4) ---------------------- MODULARITY AND MODULES To make a shirt: do we buy cotton, plastic, steel? or thread, buttons, needles? or fabric, sewing machines? To build a car: do we buy rubber, steel, plastic? or wheels, axles, etc.? or wheel assemblies, engines, etc? def: a *module* is a collection of related parts. ---------------------- --------------------------- MODULES IN SOFTWARE def: a *module* is a collection of related objects (variables and constants) functions types macros usually in a separately compilable unit, which offers the possibility of hiding information. For us a module will be a .h file, and (usually) one .C file. Examples: prompt.h and prompt.C IOVec.h and IOVec.C pretend_bool.h --------------------------- ----------------------------- INFORMATION HIDING client =================== module interface (.h) implementation (.C) ------------------------------- does this resemble any picture we've seen before? ------------------------------- // Sort.C -- client #include "swap.h" ... swap(vec[i], vec[j]); ... // swap.h -- interface extern void sway(int & x, int & y); // MODIFIES: x, y // POST: x is y and y is x // swap.C -- implementation #include "assign2.h" #include "swap.h" void swap(int& x, int& y) { assign2(x, y, y, x); } ----------------------------- B. why to use them 1. reuse (2.2) ---------------------- WHY USE MODULES? Possibility of off-the-shelf components - don't have to reinvent algorithms - can write at a higher level - saves money - programs more reliable - programs more efficient ---------------------- 2. locality ---------------------------- Module specification is a contract - allows division of labor - separation of concerns - implementation independence - assignment of blame ---------------------------- C. how to use them (2.4) 1. information hiding, file scope, and linkage in C++ (2.7) --------------------------------- WHAT CAN BE VISIBLE TO CLIENTS What can be visible when linking (.C): variables functions (constants) Not involved at link time: typedefs enum #define ---------------------------------- ----------------------------------- INFORMATION HIDING WITH C++ To Hide (from linkers): static int myPrivateVar; static void MyOwnFunction(int & x) // POST: x == myPrivateVar { // ... } const int PRIVATE_NUM = 3; To make visible to clients (linkers): int myPublicVar; void FunForYou(int & x) // POST: x == myPublicVar { // ... } extern const int VISIBLE_NUM = 4; ------------------------------------ 2. guidlines for header files (p.67, p.87) ---------------------------------- WHAT GOES IN A HEADER (.h) FILE // Example.h #ifndef Example_h #define Example_h 1 #include const char AT_SIGN = '@'; typedef char string[]; enum Error {IO, RANGE, ACCESS}; extern void swap(int & x, int & y); // MODIFIES: x, y // POST: x is y and y is x extern float delta; #endif ---------------------------------- ------------------------------- MULTIPLE INCLUSIONS // foo.h #include "Example.h" // bar.h #include "Example.h" // typical.C #include "foo.h" #include "bar.h" Example.h / \ foo.h bar.h \ / typical.C #ifndef Example_h #define Example_h 1 ... #endif #ifndef Example_h #define Example_h 1 ... #endif ------------------------------- ------------------------ WHAT GOES IN .C FILES // Example.C #include "Example.h" #include "assign.h" void swap(int & x, int & y) { assign2(x, y, y, x); } float delta = 3.4; ------------------------ 3. examples 4. using libraries ---------------------------- NEED FOR LIBRARIES # Makefile PUB = /home/cs228/public CPP = $(PUB)/bin/ansicpp OBJECTS = selsort.o Sort.o IOVec.o \ sort.o prompt.o selsort.exe: $(OBJECTS) $(CPP) -o selsort.exe $(OBJECTS) Sort ------> swap ------> assign2 uses uses -------------------------------- -------------------------------- MAKEFILE WITH LIBRARIES #Makefile LIB = $(PUB)/lib LIBS = -L$(PUB)/lib -lcs228 HDIR = $(PUB)/include INCLUDES = -I$(HDIR) OBJECTS = selsort.o Sort.o IOVec.o selsort.exe: $(OBJECTS) $(CPP) -o selsort.exe $(OBJECTS) \ $(LIBS) Sort.C: Sort.h swap.h $(CPP) -c $(INCLUDES) Sort.C ---------------------------- II. C++ library modules (HR 2.8) (may omit if little time) ----------------------------- C++ BUILT-IN MODULES extern double cos(double x); ... extern int abs(int i); extern void exit (int exitStatus); ... extern int isalpha(int ch); extern int isdigit(int ch); ... extern unsigned strlen(const char str[]); #define assert(conditional_exp) ... -----------------------------