Libraries Making Functions Reuseable. Review Last time, we wrote a program to compute the area and circumference of an ellipse. a a bb.

Slides:



Advertisements
Similar presentations
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Parameter Passing Mechanisms Reference Parameters.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Function basics – pp Computational assistants – improves clarity and enable software reuse. JPC and JWD © 2002 McGraw-Hill, Inc.
Computer Programming 1 Problem Solving and Software Engineering.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Computer Science 1620 Programming & Problem Solving.
Functions Chapter 4. C++ An Introduction to Programming, 3rd ed. 2 Objectives Study software development using OCD Take a first look at building functions.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
Scope Accessibility of Names. Review We’ve seen that C++ permits a programmer to declare names and then use those names in a manner consistent with their.
Libraries Making Functions Globally Reusable (§ 6.4) 1.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Controlling Function Behavior Sequence, Selection and Repetition.
Parameter Passing Mechanisms Reference Parameters Read § §
Libraries 1 Making Functions Globally Reusable (§ 4.6)
1 Simple Functions Writing Reuseable Formulas. In Math Suppose f (x) = 2 x 2 +5Suppose f (x) = 2 x 2 +5 f(5)=?f(5)=? f(5) = 2* =55f(5) = 2*
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.
1 Chapter-01 Introduction to Software Engineering.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
Comp 245 Data Structures (A)bstract (D)ata (T)ypes ADT.
1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 05, 2005 Lecture Number: 4.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Functions Chapter 4. C++ An Introduction to Programming, 3rd ed. 2 Objectives Study software development using OCD Take a first look at building functions.
Problem Session Working in pairs of two, solve the following problem...
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
1 Chapter-01 Introduction to Software Engineering.
Libraries 1 Making Functions Globally Reusable (§ 6.4)
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Software Engineering Algorithms, Compilers, & Lifecycle.
Chapter Topics The Basics of a C++ Program Data Types
Introduction to Programmer-Defined Functions
Engineering Problem Solving With C An Object Based Approach
Basic Elements of C++.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Writing Reuseable Formulas
solve the following problem...
Basic Elements of C++ Chapter 2.
Separate Compilation and Namespaces
Screen output // Definition and use of variables
Wednesday 09/23/13.
C++ Compilation Model C++ is a compiled language
Chapter 6: User-Defined Functions I
Functions Imran Rashid CTO at ManiWeber Technologies.
CS 144 Advanced C++ Programming January 31 Class Meeting
What Is? function predefined, programmer-defined
Object-Centered Design
SPL – PS1 Introduction to C++.
Presentation transcript:

Libraries Making Functions Reuseable

Review Last time, we wrote a program to compute the area and circumference of an ellipse. a a bb

Recall We spent significant time and effort writing functions for ellipse area and circumference. double EllipseArea(double length, double width); { double halfLength = length/2.0, halfWidth = width/2.0; return PI * halfLength * halfWidth; } double EllipseCircumference(double length, double width); { double halfLength = length/2.0, halfWidth = width/2.0; return 2.0 * PI * sqrt((pow(halfLength, 2.0) + pow(halfWidth, 2.0))/2.0); }

Problem Suppose that in order to solve a different problem, a different program needs to compute the area of an ellipse? Options: –Copy-and-paste EllipseArea() from our previous program into the new program... +Store EllipseArea() and EllipseCircumference() in a library module, so that programs can share them.

Library Modules A library module consists of three files: A header file (usually with a suffix) containing shareable function prototypes.A header file (usually with a.h suffix) containing shareable function prototypes. An implementation file (with suffix ) containing shareable function definitions.An implementation file (with suffix.cpp ) containing shareable function definitions. A documentation file (with suffix ) containing documentation for the library.A documentation file (with suffix.doc ) containing documentation for the library.

Example Since we are creating a library to share functions that describe an ellipse, we name our library ellipse, with header file,header file ellipse.h, implementation file, andimplementation file ellipse.cpp, and documentation file.documentation file ellipse.doc.

ellipse.h In, we place the function prototypes: In ellipse.h, we place the function prototypes: /* ellipse.h * Author: J. Adams. * Date: January * Purpose: Functions for computing ellipse attributes. */ double EllipseArea(double length, double width); double EllipseCircumference(double length, double width); //... plus any others we want to provide while we’re at it To use these functions, a program must contain #include ”ellipse.h”

ellipse.cpp Their definitions are placed in : Their definitions are placed in ellipse.cpp : /* ellipse.cpp * Author: J. Adams * Date: January * Purpose: Functions for computing ellipse attributes. */ const PI = ; double EllipseArea(double length, double width); { double halfLength = length/2.0, halfWidth = width/2.0; return PI * halfLength * halfWidth; }...

ellipse.cpp (cnt’d)... double EllipseCircumference(double length, double width); { double halfLength = length/2.0, halfWidth = width/2.0; return 2.0 * PI * sqrt((pow(halfLength, 2.0) + pow(halfWidth, 2.0))/2.0); } This file can be compiled separately from any program that uses it.

ellipse.doc The documentation file is a copy of the header file, plus function specifications: /* ellipse.doc * Author: J. Adams. * Date: January * Purpose: Functions for computing ellipse attributes. */ /********************************************************* * Compute the area of an ellipse. * * Receive: length, width, two double values. * * Return: the area of the corresponding ellipse. * *********************************************************/ double EllipseArea(double length, double width);

ellipse.doc (cnt’d) //... first part of ellipse.doc /********************************************************* * Compute the circumference of an ellipse. * * Receive: length, width, two double values. * * Return: the circumference of ellipse defined by * * length and width. * *********************************************************/ double EllipseCircumference(double length, double width); //... plus any others we want to provide while we’re at it By storing the documentation in a separate file, we provide information on how to use the library without cluttering its header file.

#include When the C++ compiler processes a directive, it When the C++ compiler processes a #include directive, it opens the file named in the directive; andopens the file named in the directive; and replaces the directive with the contents of that file.replaces the #include directive with the contents of that file. When the file contains function prototypes, the effect of the directive is to insert those prototypes into the program. When the file contains function prototypes, the effect of the #include directive is to insert those prototypes into the program.

Translation Translating a program into machine language consists of two steps: 1. Compilation, in which the program is converted into the computer’s machine language; and 2. Linking, in which any calls to functions defined outside of that program are bound to function definitions.

Translation (cnt’d) Compilation creates a binary object file (usually with a or suffix). Compilation creates a binary object file (usually with a.o or.obj suffix). Linking binds multiple object files into a single binary executable file that can be run. int main() { //... } C++ Compiler file.cppfile.obj

Linking C++ Linker file1.obj file.exe file2.obj fileN.obj

Using a Library To use the library, a program must #include its header file (usually above the main function), inserting the function prototypes so that they can be called.#include its header file (usually above the main function), inserting the function prototypes so that they can be called. be linked to its implementation file.be linked to its implementation file. Failure to do either of these produces an error.

Example #include // cin, cout, >,... #include // sqrt(), pow(),... using namespace std; #include “ellipse.h” // insert ellipse prototypes int main() { cout << “\nTo compute the area and circumference” << “\n of an ellipse, enter its length and width: ”; double length, width; cin >> length >> width; double area = EllipseArea(length, width); double circumference = EllipseCircumference(length,width); cout << “\nThe area is “ << area << “\n and the circumference is “ << circumference << endl; }

Compiler Errors A compiler error occurs if a program calls a function for which no prototype is given. This can occur if you call a library function and neglect to the header file containing its prototype. This can occur if you call a library function and neglect to #include the header file containing its prototype. - You’ll be trying to use a function that has not been declared.

Linker Errors A linker error occurs if a program calls a function for which the linker is unable to find a definition. This can occur if a program calls a function but the linker is not told to use the library implementation file or object file containing that function’s definition. How this is done varies from platform to platform, but often involves a project file.

OCD with Libraries 1. Specify the desired behavior of the program. 2. Identify the objects needed. 3. Identify the operations. a. If an operation is not predefined: Write a function to perform it. b. If an operation is likely to be reuseable someday: Store its function in a library and access it from there. 4. Organize objects and operations into an algorith m.

Summary Functions provide a programmer with a means of extending C++ with new operations that are not predefined in the language. Libraries provide a means of storing functions so that multiple programs can use them. Libraries consist of three files: for function prototypes, definitions, and documentation.

Summary (Cnt’d) To use a library, a program must #include its header file, or a compiler error will occur.#include its header file, or a compiler error will occur. Be linked to its implementation file (or the object file produced by compiling the implementation file), or a linker error will occur.Be linked to its implementation file (or the object file produced by compiling the implementation file), or a linker error will occur.