Libraries Making Functions Globally Reusable (§ 6.4) 1.

Slides:



Advertisements
Similar presentations
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Advertisements

Parameter Passing Mechanisms Reference Parameters.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Computer Programming 1 Functions. Computer Programming 2 Objectives Take a first look at building functions Study how a function is called Investigate.
Computer Science 1620 Programming & Problem Solving.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Guide To UNIX Using Linux Third Edition
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages Updated by: Dr\Ali-Alnajjar.
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.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
1 Programming and Problem Solving — Software Engineering (Read Chap. 2)
Programming and Problem Solving — Software Engineering (Read Chap. 2) 1.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Controlling Function Behavior Sequence, Selection and Repetition.
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*
CPS120: Introduction to Computer Science Functions.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
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.
1 Simple Methods Chap. 4 Study Sections 4.1 – 4.4 Writing Reusable Formulas.
Functions Chapter 4. C++ An Introduction to Programming, 3rd ed. 2 Objectives Study software development using OCD Take a first look at building functions.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Libraries Making Functions Reuseable. Review Last time, we wrote a program to compute the area and circumference of an ellipse. a a bb.
1 Simple Methods Chap. 4 Study Sections 4.1 – 4.4 Writing Reusable Formulas.
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.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
1 Chapter-01 Introduction to Software Engineering.
The Development Process Compilation. Compilation - Dr. Craig A. Struble 2 Programming Process Problem Solving Phase We will spend significant time on.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 3 Part II. 3.8 Placing a Class in a Separate File for Reusability.cpp file is known as a source-code file. Header files ◦ Separate files in which.
Libraries 1 Making Functions Globally Reusable (§ 6.4)
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Software Engineering Algorithms, Compilers, & Lifecycle.
What Is? function predefined, programmer-defined
Introduction to Programmer-Defined Functions
Separate Compilation and Namespaces
Writing Reuseable Formulas
Separate Compilation and Namespaces
Separate Compilation.
C Preprocessor(CPP).
C++ Compilation Model C++ is a compiled language
Separate Compilation.
C++ Programming Basics
What Is? function predefined, programmer-defined
Chapter 1 c++ structure C++ Input / Output
SPL – PS1 Introduction to C++.
Presentation transcript:

Libraries Making Functions Globally Reusable (§ 6.4) 1

Review a a bb 2 We have written a program that used functions to compute the area and circumference of ellipses.

3 double ellipseArea(double length, double width); double ellipseCircumference(double length, double width); Function Prototypes (placed before main() ): Function Definitions & Documentation (placed after main() ): const double PI = ; /* Function to compute the area of an ellipse Receive: two double values length, width, representing the major axis and minor axis of an ellipse Return: the area of the ellipse */ double ellipseArea(double length, double width) { double halfLength = length/2.0, halfWidth = width/2.0; return PI * halfLength * halfWidth; }

4 Putting these prototypes, documentation, and definitions along with main() in the same program makes it possible to reuse them at several different places in the program. However, suppose that in order to solve some other problem, a different program requires computing the area and circumference of an ellipse. double ellipseCircumference(double length, double width) { double halfLength = length/2.0, halfWidth = width/2.0; return 2.0 * PI * sqrt( (halfLength * halfLength + halfWidth * halfWidth) / 2.0); } /* Function to compute the circumference of an ellipse Receive: two double values length, width, representing the major axis and minor axis of an ellipse Return: the circumference of the ellipse */ Funcs call others Proj 5 area -> perim Proj.6: many

How can we reuse our functions in a different program? Options: Copy-and-paste ellipseArea() and ellipseCircumference() from our previous program into the new program. Store ellipseArea() and ellipseCircumference() in a library so that programs can share them. 5 Is there automatic updating of a program if the original functions are modified? No Yes! We also have global reusability. OLE

Libraries A library consists of three files: A header file (whose name has a.h suffix) that contains shareable function prototypes and constants. An implementation file (whose name has a.cpp suffix) that contains shareable function definitions. A documentation file (whose name has a.txt (or.doc ) suffix) that contains documentation for the library. 6 In Visual C++, put all 3 and the program that uses the library in the same project. Source Files Resource Files Header Files What? How? Detailed what - user man. 6

Example 7 Other Examples: Text: Temperature conversion Project: Metric conversion Since we are creating a library to share functions that describe an ellipse, we might name our library ellipse, with header file ellipse.h, implementation file ellipse.cpp, and documentation file ellipse.txt. must be the same Temp – good e.g. to look at

Header file: ellipse. h In ellipse.h, we place the function prototypes: /*----- ellipse.h Library of functions for computing ellipse attributes. L. Nyhoff CS 104X Jan 16, 2012 Functions provided: ellipseArea: compute area of an ellipse ellipseCircumference: compute circumference of an ellipse */ double ellipseArea(double length, double width); double ellipseCircumference(double length, double width); //... plus any others we want to provide such as the //... focal length, eccentricity, etc. The corresponding implementation file ellipse.cpp and any program that uses these functions must contain #include "ellipse.h" 8 Note the "directory" Can use this same opening documentation in all 3 files but change 1st line What? How? Detailed what - user man opening doc: dict-ary;.cpp encyclop. public Metric

Their definitions are placed in ellipse.cpp : 9 /*----- ellipse.cpp Library of functions for computing ellipse attributes. L. Nyhoff CS 104X Jan 16, 2012 Functions provided: ellipseArea: compute area of an ellipse ellipseCircumference: compute circumference of an ellipse */ #include using namespace std; #include "ellipse.h" const double PI = ; // Could go in ellipse.h double ellipseArea(double length, double width) { assert(length >= 0 && width >= 0); double halfLength = length/2.0, halfWidth = width/2.0; return PI * halfLength * halfWidth; } Put after using namespace std; Implementation file: ellipse.cpp #include.h - compiler check func protos with defs. here; can chamge implem.

double ellipseCircumference(double length, double width) { assert(length >= 0 && width >= 0); double halfLength = length/2.0, halfWidth = width/2.0; return 2.0 * PI * sqrt((halfLength * halfLength + halfWidth * halfWidth)/2.0); } //... plus definitions of any others we provide... This file can be compiled separately from any program that uses it 10 Implementation file (cont.) (called a client program). Metric Not build -- private

Our documentation file will be a copy of the header file with function specifications added for additional documentation. /*----- ellipse.txt Library of functions for computing ellipse attributes. L. Nyhoff CS 104X Jan 16, 2012 Functions provided: ellipseArea: compute area of an ellipse ellipseCircumference: compute circumference of an ellipse */ /* 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); 11 Documentation file: ellipse.txt

/* 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 prototypes and specifications //... for any others we provide But some programmers do put the documentation in the header file. Documentation file (cont.) By storing the documentation in a separate file, we provide information on how to use the library without cluttering the other library files. Metric public

Program Translation Translating a program into machine language consists of two steps: 1. Compiling, in which the syntax of the main program and of the implementation files of any included libraries are checked, and if no errors are found, converts them into the computer’s machine language. 2. Linking, in which any calls to functions (from main() or from other functions) are bound to the definitions of those functions. 13 link: now defs outside program compile & link diagram

Using a Library 14 To use a library: A program must #include its header file (usually above the main function and after using namespace std; ). When the compiler (actually its preprocessor) encounters this #include directive, it must be able to find the header file so it can open it and replace the #include directive with its contents so they get compiled along with the program. When the file contains function prototypes, the effect of the #include directive is to insert those prototypes into the program. Put it in the project's header files In Visual C++: Metric

15 It must also be able to find the corresponding implementation file so it can open it, insert the contents of its header file into it, and then compile it (separately from the program). Put it in the project's source files In Visual C++: Once these compilations are successful, the linker combines these compiled files into one, connecting (i.e., "linking") each function call to the compiled code of that function's definition. A failure in either stage is an error.

Compilation Errors A program calling a function for which no prototype is given produces a compiler error. 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. 16 what proto does!

Linking Errors A program calling a function for which the linker cannot find a definition produces a linker error. 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. 17

Example #include // cin, cout, >,... using namespace std; #include "ellipse.h" // insert ellipse prototypes int main() { cout << "Program to compute the area and circumference of ellipses.\n"; double majorAxis, minorAxis; cout << "\nEnter major & minor axes (in meters) (0 0 to stop): "; cin >> majorAxis >> minorAxis; while (majorAxis > 0) { double area = ellipseArea(majorAxis, minorAxis); double circumference = ellipseCircumference(majorAxis, minorAxis); cout << "\nFor an ellipse with major axis " << majorAxis << " meters and minor axis " << minorAxis << " meters\n" << "\tarea = " << area << " sq. meters\n" << "\tcircumference = " << circumference << " meters\n"; cout << "\nEnter major & minor axes (in meters) (0 0 to stop): "; cin >> majorAxis >> minorAxis; } 18 Put after using namespace std; Temperature conversion example in the text Metric

1> Rebuild All started: Project: EllipseLib, Configuration: Debug Win > ellipse.cpp 1> driver.cpp 1> Generating Code... 1> EllipseLib.vcxproj -> E:\CS Int\classprogs\EllipseLib\Debug\EllipseLib.exe ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ========== 19 EXECUTION: Program to compute the area and circumference of ellipses. Enter major & minor axes (in meters) (0 0 to stop): 2 2 For an ellipse with major axis 2 meters and minor axis 2 meters area = sq. meters circumference = meters Enter major & minor axes (in meters) (0 0 to stop): 0 0 Press any key to continue...

Compilation creates a binary object file (usually with a.o or.obj suffix) from a.cpp file. C++ Compiler int main() { //... } file.cpp file.obj 20

file1.obj file.exe file2.obj fileN.obj C++ Linker Linking binds multiple object files into a single binary executable file that can be executed by the operating system. 21

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 reusable someday: Store its function in a library and access it from there. 4. Organize objects and operations into an algorithm. 22 Extending C++

/*----- ellipse.h Library of functions for computing ellipse attributes. L. Nyhoff CS 104X Jan 16, 2012 Functions provided: ellipseArea: compute area of an ellipse ellipseCircumference: compute circumference of an ellipse */ double ellipseArea(double length, double width); double ellipseCircumference(double length, double width); //... plus any others we want to provide such as the //... focal length, eccentricity, etc. 23

24 /*----- ellipse.cpp Library of functions for computing ellipse attributes. L. Nyhoff CS 104X Jan 16, 2012 Functions provided: ellipseArea: compute area of an ellipse ellipseCircumference: compute circumference of an ellipse */ #include using namespace std; #include "ellipse.h” const double PI = ; // Could go in ellipse.h double ellipseArea(double length, double width) { assert(length >= 0 && width >= 0); double halfLength = length/2.0, halfWidth = width/2.0; return PI * halfLength * halfWidth; } double ellipseCircumference(double length, double width) { assert(length >= 0 && width >= 0); double halfLength = length/2.0, halfWidth = width/2.0; return 2.0 * PI * sqrt((halfLength * halfLength + halfWidth * halfWidth)/2.0); } //... plus definitions of any others we provide...

double ellipseCircumference(double length, double width) { assert(length >= 0 && width >= 0); double halfLength = length/2.0, halfWidth = width/2.0; return 2.0 * PI * sqrt((halfLength * halfLength + halfWidth * halfWidth)/2.0); } //... plus definitions of any others we provide... 25