Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.

Similar presentations


Presentation on theme: "Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments."— Presentation transcript:

1 Unit 10 Code Reuse

2 Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments Macros C++ features

3 Procedural Abstraction What a function does Design the solution to the problem – Define the algorithm – Associate a function with each step How it does it Implementation – Implement each function – Can be implemented by different programmer because the user is only concerned with what the function does

4 Data Abstraction Logical View What data is stored Operations performed on the data Physical View How data is stored Physical storage of data type varies by computer double +, -, *, /, =, ==,, =

5 Information Hiding and Encapsulation Information hiding – Implementation details of a lower-level module are hidden from a higher-level module. – Example: You don't know how fopen operates. Encapsulation – Grouping functions into personal libraries – Allows code reuse between applications

6 Figure 13.1 Preparing a Program for Execution

7 Creating a Personal Library 1.Create a header (.h) file. 2.Create an implementation file. 3.Compile the implementation file. 4.Include the.h file in the programs that use the library: #include "myLibrary.h"

8 Figure 13.2 Header File planet.h for Personal Library with Data Type and Associated Functions

9

10 Figure 13.3 Portion of Program That Uses Functions from a Personal Library

11 Implementation File Should contain: – Comment summarizing library's purpose – #include directive for the library's header – #include direction for other libraries used – #define directives for constants used inside the library, but not by the program that uses the library – Type definitions used by the library that are not visible to the program that uses the library – Function definitions

12 Figure 13.4 Implementation File planet.c Containing Library with Planet Data Type and Operators

13 Figure 13.4 Implementation File planet.c Containing Library with Planet Data Type and Operators (cont’d)

14 Storage Classes auto – Default storage class (no keyword necessary) – Available from time declared until end of function extern – Globally available – Used in libraries – Avoid except where necessary static – Initialized when program starts – Retains changes across multiple function calls register – Not respected by all compilers – Can optimize frequently used variables

15 Figure 13.7 Use of Variables of Storage Class extern

16 Global Variable Access Examples

17 Static Variables int fun_frag(int n) { static int once = 0; int many = 0; once++; many++; } Increments each time function is called

18 Static and Register with Large Arrays Declare large arrays as static to prevent them from being loaded on the stack. Declare subscript variables as register for fast access: static double matrix[50][40]; register int row, col;

19 exit Function Exits a program prematurely. Pass 0 to indicate normal completion. Pass 1 to indicate a failure. Exit condition constants: – EXIT_SUCCESS – EXIT_FAILURE

20 Figure 13.8 Function factorial with Premature Exit on Negative Data

21 Conditional Compilation Used to selectively compiled code. – Compile debug statements only when debugging. – Prevent libraries from being included multiple times. Define a constant. – #define Undefine a constant. – #undef Check whether a constant is defined. – #if defined (constant) – #elif – #endif

22 Figure 13.9 Conditional Compilation of Tracing printf Calls

23 Figure 13.10 Conditional Compilation of Tracing printf Calls

24 Figure 13.11 Header File That Protects Itself from Effects of Duplicate Inclusion

25 Figure 13.11 Header File That Protects Itself from Effects of Duplicate Inclusion (cont’d)

26

27 Command-Line Arguments Add two arguments to function main: – int arg c number of arguments – char *argv[] argument vector arg[0] is the program name Function main syntax: int main(int argc, char *argv[]) { }

28 Figure 13.12 File Backup Using Arguments to Function main

29 Macros Facility for naming a commonly used statement or operation Expanded by the preprocessor Syntax: #define macro_name(parameterlist) body

30 Figure 13.13 Program Using a Macro with Formal Parameters

31 Figure 13.14 Macro Expansion of Second Macro Call of Program in Fig. 13.13

32 Figure 13.15 Macro Calls Showing Importance of Parentheses in Macro Body

33 Figure 13.15 Macro Calls Showing Importance of Parentheses in Macro Body (cont’d)

34 Figure 13.16 Macro Expansions of Macro Calls from Fig. 13.15

35 Figure 16.1 Comparison of (a) C and (b) C++ Control Structures

36 Figure 16.1 Comparison of (a) C and (b) C++ Control Structures (cont’d)

37 Figure 16.2 Implementing Output Parameters in C and C++

38 Object-Oriented Programming Class definition Operator overloading Function overloading Polymorphism

39 Figure 16.3 “Donut” Model of an Abstract Data Type

40 Figure 16.4 “Donut” Model of Standard Type int

41 Figure 16.5 Comparison of Models of Standard Type int and Abstract Data Type Complex

42 Figure 16.6 Header File for Class Complex

43 Figure 16.7 Implementation File for Class Complex

44 Figure 16.7 Implementa- tion File for Class Complex (cont’d)

45 Figure 16.7 Implementation File for Class Complex (cont’d)

46 Figure 16.8 Driver Function to Test Class Complex

47 Figure 16.9 Step-by-Step Evaluation of Multiple << Operations

48 Figure 16.10 Declaration of Class Ratio


Download ppt "Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments."

Similar presentations


Ads by Google