1 / 19

EEE 243B Applied Computer Programming

EEE 243B Applied Computer Programming. Modules and information hiding. Review. What are the three concepts used by humans to deal with the complexity of problems? If I have the following: int a[3] = {1,7,9}; int *p = a; // a pointer int *r = &a[1];

Télécharger la présentation

EEE 243B Applied Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EEE 243BApplied Computer Programming Modules and information hiding

  2. Review • What are the three concepts used by humans to deal with the complexity of problems? • If I have the following: int a[3] = {1,7,9}; int *p = a; // a pointer int *r = &a[1]; *p = r[0]; //What is the result of this? r[-2] = *p; //What does this do? Maj JGA Beaulieu & Capt MWP LeSauvage

  3. Outline • Modular decomposition - (the higher level) • Information hiding • The C module • The C module - function prototypes • A small example • Building with components Maj JGA Beaulieu & Capt MWP LeSauvage

  4. Modular decomposition • So far we have seen that we can decompose a large monolithic function (main) into many smaller and cohesive functions • Up to now, all of the functions that we have defined ourselves, have all been defined into one program file; a compilation unit • We have also learned what a function prototype is, and how to declare one within our program file. Maj JGA Beaulieu & Capt MWP LeSauvage

  5. Modular decomposition • Large computer programs just do not appear out of nowhere • Well designed software, like any other products of engineering, should be designed with a philosophy of planning and careful implementation • Similar to an office building, a software program should have a well-designed structure; an architecture Maj JGA Beaulieu & Capt MWP LeSauvage

  6. Modular decomposition • For large programs, it is not sufficient to decompose a program into functions • The architecture of a large program should be expressed into separate compilation units • These separate compilation units in C are called modules • You already have used several library modules such as stdio, conio, dsound,… • You also have used a locally developed module, the 243_lib skeleton Maj JGA Beaulieu & Capt MWP LeSauvage

  7. Information Hiding • The concept of information hiding originates from a seminal paper from David Parnas (1972) • Information hiding: « Each module has a secret which it hides from other modules. » Maj JGA Beaulieu & Capt MWP LeSauvage

  8. Information Hiding • The concept of information hiding is key to the concepts of decomposition and abstraction • Hide the information that can or may change and expose a stable interfacethat will not change • The information is ONLY accessed through the interface, never directly – It is hidden Maj JGA Beaulieu & Capt MWP LeSauvage

  9. Information Hiding • Why is information hiding important? • If we encapsulate the information that can change in the module implementation and provide the service through a stable interface, then the user of the module does not need to know about the implementation • I can change the implementation to improve the performance, due to hardware/platform changes… • I therefore reduce the coupling between modules. The program is more maintainable Maj JGA Beaulieu & Capt MWP LeSauvage

  10. Information Hiding • So what is this secret I am trying to hide? There are three types of information hiding modules: • Behaviour-hiding: Hides something like screen formats, print formats, user menus, communication of data, change in state etc… • Ex: conio.h – hides the way output to screen is formatted and how keys presses are obtained • Design decision-hiding: Hides the kind of data structures and algorithms you use • Ex: tm.h – hides the algorithm and structures for task management • Machine-hiding: Hides platform specific interfacing • Ex: 243_lib.h – hides the brickOS specific functions and some types • Ex: dsound.h – hides the way sounds are played on H/W Maj JGA Beaulieu & Capt MWP LeSauvage

  11. Information Hiding • When we apply the concept of information hiding, we can separate our concerns and decompose our problem into cohesive units of compilation • All the software engineering concepts that we have discussed to this point are design concepts • The extent to which each of these concepts can be implemented in the code depends on the support provided by the language Maj JGA Beaulieu & Capt MWP LeSauvage

  12. The C Module • C provides two different kinds of files that allow us to define modules • The .h file – or module header: Contains the declaration of functions and attributes that we want other compilation units to have access to. AKA module interface • The .c file – or module body: Contains the definition of the functions that are declared in the module header as well as other internal or helper functions. AKA module implementation Maj JGA Beaulieu & Capt MWP LeSauvage

  13. The C Module • When you include a module header in your programs, only the functions and attributes that are declared in the header are available • It is NOT a good idea to give direct access to your variables inside a module Maj JGA Beaulieu & Capt MWP LeSauvage

  14. The C Module - Prototypes • A module must have two files with the same name but with the .c and .h extensions (243_lib.c, 243_lib.h) • You must declare a function prototype in the header file for each of the function that you want other modules to have access to • You can also include some derived types in the header. We will see those next week. Maj JGA Beaulieu & Capt MWP LeSauvage

  15. Example – great_small.c #include <stddef.h> #include “compare.h” void main(void) { int firstInt = 5; int secondInt = 10; int *pIntG = NULL; int *pIntS = NULL; pIntG = Greater(&firstInt,&secondInt); pIntS = Smaller(&firstInt,&secondInt); printf("Greater: %d Smaller: %d", *pIntG, *pIntS); } //main Maj JGA Beaulieu & Capt MWP LeSauvage

  16. Example – compare.h //compare.h the interface of compare blablabla #include <stddef.h> //return a pointer of type int that points //to the greater of two ints int *Greater(int *px, int *py); //return a pointer of type int that points //to the smaller of two ints int *Smaller(int *px, int *py); Maj JGA Beaulieu & Capt MWP LeSauvage

  17. Example – compare.c //compare.c the body of compare module blablabla //return a pointer of type int that points //to the greater of two ints int *Greater(int *px, int *py) { return (*px > *py ? px : py); }//Greater //return a pointer of type int that points //to the smaller of two ints int *Smaller(int *px, int *py) { return (*px < *py ? px : py); }//Smaller Maj JGA Beaulieu & Capt MWP LeSauvage

  18. Building with components • As modules are developed worldwide, libraries and reusable components emerge • These components are either full executable solutions or piece parts to be assembled into larger systems • Information hiding and abstraction make it possible to build solutions from known components without knowing how they are built • The suite of MS-OfficeTM has been redesigned with this concept in mind Maj JGA Beaulieu & Capt MWP LeSauvage

  19. Quiz Time • What is information hiding? • Why is information hiding important? • What do we mean when we say encapsulating? • A header file is also called the module _____________ • A body file is also called the module _____________ Maj JGA Beaulieu & Capt MWP LeSauvage

More Related