DOC PREVIEW
Princeton COS 217 - Modules and Interfaces

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Modules and InterfacesCS 2172Review: Constants• C has several ways to define a constant• Use #define  #define MAX_VALUE 10000 Substitution by preprocessing (will talk about this later)• Use “const” const double x = 1.56; Qualifier to declare that a variable is a constant• Declare an enumerate constant type enum color { WHITE, YELLOW, BLUE, RED };enum color c2; Offers the chance of checking3Modules• Programs are made up of many modules• Each module is small and does one thing Set, stack, queue, list, etc. String manipulation Mathematical functions• Deciding how to break up a program into modules is a key to good software design4Clients, Interfaces, Implementations• Interfaces (Application Programming Interfaces or APIs) are contracts between clients and implementations Clients must use interface correctly Implementations must do what they advertiseClientInterfaceImplementation5Interfaces• An interface defines what the module does Decouple clients from implementation Hide implementation details• An interface specifies… Data types and variables Functions that may be invokedextern int counter;extern void counter_init();extern void counter_inc();counter1.h6Interfaces• An interface defines what the module does Decouple clients from implementation Hide implementation details• An interface specifies… Data types and variables Functions that may be invokedtypedef struct {StrList *entries;int size;} StrList;extern StrList *StrList_create(void);extern void StrList_delete(StrList *list);extern void StrList_insert(StrList *list, char *string);extern void StrList_remove(StrList *list, char *string);extern int StrList_write(StrList *list);typedef struct {StrList *entries;int size;} StrList;extern StrList *StrList_create(void);extern void StrList_delete(StrList *list);extern void StrList_insert(StrList *list, char *string);extern void StrList_remove(StrList *list, char *string);extern int StrList_write(StrList *list);strlist.h:7Implementations• An implementationdefines how the module does it• Can have many implementations for one interface Different algorithms for different situations Machine dependencies, efficiency, etc.#include "counter1.h"int counter;void counter_init() {counter = 0;}void counter_inc() {counter++;}counter1.c8Implementations• An implementationdefines how the module does it• Can have many implementations for one interface Different algorithms for different situations Machine dependencies, efficiency, etc.#include “strlist.h”StrList *StrList_create(void){StrList *list = malloc(sizeof(StrList));list->entries = NULL;list->size = 0;}void StrList_delete(StrList *list){free(list);}. . .#include “strlist.h”StrList *StrList_create(void){StrList *list = malloc(sizeof(StrList));list->entries = NULL;list->size = 0;}void StrList_delete(StrList *list){free(list);}. . .9Clients• A client uses a module via its interface• Clients see only the interface Can use module without knowing its implementation• Client is unaffected if implementation changes As long as interface stays the same#include <stdio.h>#include "counter1.h"main() {counter_init();counter_inc();counter_inc();printf("%d\n", counter);}test1.c10Clients• A client uses a module via its interface• Clients see only the interface Can use module without knowing its implementation• Client is unaffected if implementation changes As long as interface stays the same#include “strlist.h”int main(){StrList *list = StrList_create();StrList_insert(list, “CS217”);StrList_insert(list, “is”);StrList_insert(list, “fun”);StrList_write(list);StrList_delete(list);}#include “strlist.h”int main(){StrList *list = StrList_create();StrList_insert(list, “CS217”);StrList_insert(list, “is”);StrList_insert(list, “fun”);StrList_write(list);StrList_delete(list);}11C Programming Conventions• Interfaces are defined in header files (.h)strlist.htypedef struct {StrList *entries;int size;} StrList;extern StrList *StrList_create(void);extern void StrList_delete(StrList *list);extern void StrList_insert(StrList *list, char *string);extern void StrList_remove(StrList *list, char *string);extern int StrList_write(StrList *list);typedef struct {StrList *entries;int size;} StrList;extern StrList *StrList_create(void);extern void StrList_delete(StrList *list);extern void StrList_insert(StrList *list, char *string);extern void StrList_remove(StrList *list, char *string);extern int StrList_write(StrList *list);extern int counter;extern void counter_init();extern void counter_inc();counter1.h12C Programming Conventions• Implementations are described in source files (.c)#include "counter1.h"int counter;void counter_init() {counter = 0;}void counter_inc() {counter++;}counter1.cstrlist.c#include “strlist.h”StrList *StrList_create(void){StrList *list = malloc(sizeof(StrList));list->entries = NULL;list->size = 0;}void StrList_delete(StrList *list){free(list);}. . .#include “strlist.h”StrList *StrList_create(void){StrList *list = malloc(sizeof(StrList));list->entries = NULL;list->size = 0;}void StrList_delete(StrList *list){free(list);}. . .13#include <stdio.h>#include "counter1.h"main() {counter_init();counter_inc();counter_inc();printf("%d\n", counter);}C Programming Conventions• Clients “include” header filesmain.c#include “strlist.h”int main(){StrList *list = StrList_create();StrList_insert(list, “CS217”);StrList_insert(list, “is”);StrList_insert(list, “fun”);StrList_write(list);StrList_delete(list);}#include “strlist.h”int main(){StrList *list = StrList_create();StrList_insert(list, “CS217”);StrList_insert(list, “is”);StrList_insert(list, “fun”);StrList_write(list);StrList_delete(list);}test1.c14Standard C Librariesassert.hctype.herrno.hmath.hlimits.hsignal.hstdarg.hstddef.hstdio.hstdlib.hstring.htime.hassertionscharacter mappingserror numbersmath functionsmetrics for intssignal handlingvariable length arg listsstandard definitionsstandard I/Ostandard library functionsstring functionsdate/type functions15Standard C Libraries, cont’d• Utility functions stdlib.hatof, atoi, rand, qsort, getenv, calloc, malloc, free, abort, exit• String handling string.hstrcmp, strncmp, strcpy, strncpy, strcat,strncat, strchr, strlen, memcpy, memcmp• Character classifications ctypes.hisdigit, isalpha, isspace, isupper, islower• Mathematical functions math.hsin, cos, tan,


View Full Document

Princeton COS 217 - Modules and Interfaces

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Modules and Interfaces
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Modules and Interfaces and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Modules and Interfaces 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?