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

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Using Abstraction to Manage Complexity Abstraction: procedural abstraction & data abstraction. Procedural abstraction=> function development should separate.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
TDBA66, Lecture Ch. 11, vt-03 1 Abstraction Prucedural abstraction moduralize by hiding the details in a function() Ex.functions in get_params(…) display_match(…)
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
C++ for Engineers and Scientists Third Edition
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
chap13 Chapter 13 Programming in the Large.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 13: Programming in the Large Problem Solving & Program Design.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
2Object-Oriented Program Development Using C++ 3 Function Declarations and Definitions Analogize functions to methods –Share same basic features –Unlike.
Programming Fundamentals Enumerations and Functions.
CHAPTER 8 Scope, Lifetime, and More on Functions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Appendix 1 - Packages Jim Fawcett copyright (c)
Chapter 6 CS 3370 – C++ Functions.
CISC105 – General Computer Science
14. THE PREPROCESSOR.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Pre-processor Directives
Chapter 5 - Functions Outline 5.1 Introduction
User-defined Functions
Scope, Parameter Passing, Storage Specifiers
Functions.
6 Chapter Functions.
User-defined Functions
Dr. Bhargavi Dept of CS CHRIST
Based on slides created by Bjarne Stroustrup & Tony Gaddis
1-6 Midterm Review.
Functions Imran Rashid CTO at ManiWeber Technologies.
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
SPL – PS1 Introduction to C++.
Presentation transcript:

Unit 10 Code Reuse

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

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

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 +, -, *, /, =, ==,, =

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

Figure 13.1 Preparing a Program for Execution

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"

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

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

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

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

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

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

Figure 13.7 Use of Variables of Storage Class extern

Global Variable Access Examples

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

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;

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

Figure 13.8 Function factorial with Premature Exit on Negative Data

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

Figure 13.9 Conditional Compilation of Tracing printf Calls

Figure Conditional Compilation of Tracing printf Calls

Figure Header File That Protects Itself from Effects of Duplicate Inclusion

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

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[]) { }

Figure File Backup Using Arguments to Function main

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

Figure Program Using a Macro with Formal Parameters

Figure Macro Expansion of Second Macro Call of Program in Fig

Figure Macro Calls Showing Importance of Parentheses in Macro Body

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

Figure Macro Expansions of Macro Calls from Fig

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

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

Figure 16.2 Implementing Output Parameters in C and C++

Object-Oriented Programming Class definition Operator overloading Function overloading Polymorphism

Figure 16.3 “Donut” Model of an Abstract Data Type

Figure 16.4 “Donut” Model of Standard Type int

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

Figure 16.6 Header File for Class Complex

Figure 16.7 Implementation File for Class Complex

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

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

Figure 16.8 Driver Function to Test Class Complex

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

Figure Declaration of Class Ratio