1 / 21

EEE 243B Applied Computer Programming

EEE 243B Applied Computer Programming. Modules and Information Hiding Prof Sylvain P. Leblanc. Review. What is the error in the following: float Add ( int * anInt , float* aFloat ); void main (void) { int a; float b; myFunction (a, b); } //main

ayala
Download Presentation

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 Prof Sylvain P. Leblanc

  2. Review • What is the error in the following: • float Add (int* anInt, float* aFloat); • void main (void) • { • int a; • float b; • myFunction (a, b); • } //main • voidAdd(int* param1, float* param2) • { • return ((float)*param1 + *param2); • }//Add Decomposition, abstraction, and hierarchy First statement: a = {7,7,9} Second statement will put the value int 7 in the two bytes preceding the array, this is an error. Prof S.P. Leblanc

  3. Outline • Modular Decomposition - (the higher level) • Information Hiding • Encapsulation • What is hidden • Separation of concerns • The C module • The C module - function prototypes • A small example • Building with components Prof S.P. Leblanc

  4. 1. 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 have been defined into one program file • a single compilation unit • We have also learned about function prototypes, and how to declare them within our program file. Prof S.P. Leblanc

  5. 1. Modular decomposition • Large computer programs do not appear out of thin air! • Well designed software, like any other product 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 Prof S.P. Leblanc

  6. 1. Modular decomposition • it is not sufficient to decompose a large 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, string,… Prof S.P. Leblanc

  7. 2. 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" Prof S.P. Leblanc

  8. 2a. Encapsulation • The concept of information hiding is key to the concepts of decomposition and abstraction • Hide the information that can or may change and exposea stable interfacethat will NOTchange • This is known as encapsulation • The information is ONLY accessed through the interface, never directly – It is hidden Prof S.P. Leblanc

  9. 2a. Encapsulation Why is information hiding important? • By encapsulating the information that can change in the module implementation and providing the service through a stable interface, we free the user of the module from the need to know about the implementation • The implementation can change to improve the performance • Due to hardware/platform changes… • The coupling between modules is therefore reduced. • The program is more maintainable Prof S.P. Leblanc

  10. 2b. What do we hide? There are three types of information hiding modules: • Behaviour-Hiding: Hides items such as 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: app.h– hides the SpiderRobotspecific functions and some types Prof S.P. Leblanc

  11. 2c. Separation of Concerns • When we apply the concept of information hiding, we can separate our concerns and decompose the problem into cohesive units of compilation • All the software engineering concepts that we have discussed to this point are design concepts • The support provided by the language dictates the extent to which each of these concepts can be implemented in the code Prof S.P. Leblanc

  12. 3. 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 the 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 the module implementation Prof S.P. Leblanc

  13. 3. 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 NEVER a good idea to give direct access to your variables inside a module • Certainly, it is not a good approach to, say, a question on a midterm. Prof S.P. Leblanc

  14. 4. The C Module - Prototypes • A module must have two files with the same name but with the .c and .h extensions • app.c, app.h • You must declare a function prototype in the header file for each of the functions that you want to make accessible to other modules • You can also include some derived types in the header. • We will see those later. Prof S.P. Leblanc

  15. 5. Example – great_small.c #include <stddef.h> #include "compare.h" void main(void) { intfirstInt = 5; intsecondInt = 10; int* pIntG= NULL; int* pIntS= NULL; pIntG = Greater(&firstInt,&secondInt); pIntS = Smaller(&firstInt,&secondInt); printf("Greater: %d Smaller: %d\n", *pIntG, *pIntS); system("PAUSE"); exit(0); } //main Prof S.P. Leblanc

  16. 5. 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); Prof S.P. Leblanc

  17. 5. 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 Prof S.P. Leblanc

  18. 6. Building with components • As modules are developed worldwide, libraries and reusable components emerge • These components are either full executable solutions or 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 • There are many examples of such component builds: • Windows API • dll • etc. Prof S.P. Leblanc

  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 _____________ Every module hides their secrets from other modules. It is important because we want to increase the maintainability of our programs by reducing the effects of cascading changes. Hiding HOW we implement our solutions Header file is the interface Body is the implementation Prof S.P. Leblanc

  20. References • S. Ambler, The Object Primer, 2nd Ed. • H. van Vliet, Software Engineering, 3rd Ed. *You do not have these references Prof S.P. Leblanc

  21. Next Lecture • Arrays and Pointers Prof S.P. Leblanc

More Related