Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout << x << endl << setprecision(1) << x << endl.

Slides:



Advertisements
Similar presentations
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Advertisements

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
CS-2303 System Programming Concepts
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Organizing Programs and Data 2 Lecture 7 Hartmut Kaiser
Organizing Programs and Data Lecture 5 Hartmut Kaiser
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
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.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Defining New Types Lecture 21 Hartmut Kaiser
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 CS161 Introduction to Computer Science Topic #10.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
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.
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
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.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Programming Fundamentals Enumerations and Functions.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 4 Organizing Programs and Data. Objectives Understand the use of functions in dividing a program into smaller tasks. Discover how parameters can.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
Chapter 15 - C++ As A "Better C"
Organizing Programs and Data
User-Written Functions
Chapter 1.2 Introduction to C++ Programming
Why exception handling in C++?
Variables A piece of memory set aside to store data
Chapter 5 Function Basics
Organizing Programs and Data 2
The dirty secrets of objects
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Defining New Types Lecture 22 Hartmut Kaiser
File Review Declare the File Stream Object Name
Chapter 3: Input/Output
Chapter 3 Input output.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Summary of what we learned yesterday
Functions Imran Rashid CTO at ManiWeber Technologies.
What Is? function predefined, programmer-defined
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Review for Midterm 3.
Presentation transcript:

manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout << x << endl << setprecision(1) << x << endl << setprecision(2) << x << endl << set precision(3) << x << endl << setprecision(4) << x << endl << setprecision(2) << x << endl << setprecision(1) << x << endl << setprecision(prec) << x << endl; }

OUTPUT

MODULARITY SEPARATE COMPILATION " Functions " Arguments/Parameters " Real Parameters/Formal Parameters " Reference Parameters " Const " Polymorphism clear " sort (begin, end, predicate)

FUNCTION PARAMETERS " A communication link between calling and called modules. " In calling program, they are called arguments or actual parameters. " In called program, they are called parameters or formal parameters. " Formal parameters are local to a function. Created when called and destroyed when the function exits.

RUN TIME STACK

EXCEPTION " Stop executing and pass control to an exception handler " #include " if (size == 0) throw domain_error("local helpful message");

FUNCTION ARGUMENTS " Arguments (or real parameters) can be expressions. " x = a_function(fred, sally, joan+stephen);

REFERENCE " Allows changing the calling program variable inside a function. " Syntax is to follow formal parameter name with an & " Another name for the same object " vector homework; " vector & hw = homework; " hw and homework refer to the SAME object.

REFERENCE (cont.) " Adding const lets us use the value without allowing us to change the value. (read only) " vector homework; " vector & hw = homework; " const vector & chw = hw; " All three refer to the SAME object. hw and homework allow read or write, chw allows only read.

OVERLOADING " They do two definitions of the grade function. " This is called overloading. " The compiler can tell the difference because there is a difference in the type of the third parameter.

ERROR CHECKING " Checking for errors and throwing exceptions with helpful messages depends on where in the hierarchy you are. Sometimes, the calling program has more information about an error and the called program just needs to detect it.

USING REFERENCE " Book mentions returning a value from a function using a reference parameter. " Many people believe this is bad style. " A function should return only one value to the calling program via its name. All other parameters should be in only. " A procedure should be used if you are returning more than one value. None are returned by its name since a procedure is of type void.

MORE ON REFERENCE " If you remove the const from the reference parameter, you are saying you expect the function to change the values in the calling program. " Note, you don't put any & on the calling arguments (real parameters) or on the use of the formal parameters inside the function. Only on the definition of the formal parameters in the function statement itself.

LVALUE " lvalues are non temporary objects. " A variable is an lvalue. " An expression is not an lvalue. " Don't pass expressions to reference parameters.

READ FUNCTION EXAMPLE istream& read_hw(istream& in, vector & hw) { if(in) { hw.clear(); double x; while (in >> x) hw.push_back(x); in.clear(); } return in; }

READ FUNCTION EXAMPLE istream& read_hw(istream& in, vector & hw) { if(in) { hw.clear(); double x; while (in >> x) hw.push_back(x); in.clear(); } return in; } ALLOWS US TO CALL THE FUNCTION IN AN IF AS IN if(read_hw(cin,homework)) AND AN EVEN NEATER USE LATER

READ FUNCTION EXAMPLE istream& read_hw(istream& in, vector & hw) { if(in) { hw.clear(); double x; while (in >> x) hw.push_back(x); in.clear(); } return in; } BOTH PARAMETERS ARE REFERENCES WHICH MEANS ANY CHANGES IN THIS FUNCTION ARE DONE TO THE ARGUMENT IN THE CALLING PROGRAM. STORES DATA READ BACK IN CALLING PROGRAM'S VECTOR

READ FUNCTION EXAMPLE istream& read_hw(istream& in, vector & hw) { if(in) { hw.clear(); double x; while (in >> x) hw.push_back(x); in.clear(); } return in; } VERIFYS IT IS O.K. TO TRY A NEW INPUT. CLEARS ANY REASON THAT STOPPED INPUT CLEARS OUT ANY PREVIOUS VALUES IN HOMEWORK VECTOR

POLYMORPHIC CLEAR " Previous function shows a good example of polymorphism. " The message clear was sent to two different objects. " These two different objects responded to the message in two different ways. " hw.clear (cleared out the homework vector) " in.clear (cleared out the input stream system)

THREE PARAMETER TYPES " In the median function, we used a parameter of type vector to copy the vector into the function. This allowed the function to manipulate the copy without changing the original calling program data. " In the grade function, we used a parameter of type const vector & to define a reference. The & says don't copy and the const promises the function won't change the original data. (read only)

THREE PARAMETER TYPES " In the read_hw function we used a parameter of type vector & to define a reference that says you want to change data in the calling program.

TRY - CATCH try { // call grade // output statements } catch (domain_error) { // please try again message } TRY STATEMENTS CATCH CLAUSE IF A domain_error OCCURS, IT STOPS THE PROGRAM BY RETURNING A 1 (return 1;)

STUDENT STRUCTURE " In the final example, they build a structure for the student name, the mid-term exam score, the final exam score, and the homework vector. (similar to our lab) " Then they define a vector of these structures to store all the student data " vector students;

READING STRUCTURES istream& read(istream& is, Student_info& s) { // read and store name and exam grades is >> s.name >> s.midterm >> s.final; // read and store all the student's homework grades read_hw(is, s.homework); return is; }

USING CIN WHERE IT IS " Note because of the way they defined read_hw they can just call it to read a set of homework grades from the current position of cin.

CATCHING AN EXCEPTION " If you don't catch an exception in a function, it passes on out to the next calling function.

SORT " Now to use the sort function, you need to add one more parameter to the sort call. " This third parameter is a predicate (returning true or false) and is used to compare the items in the vector. " For integers or doubles, this can be the default comparison (leave off the third parameter) since the system knows how to compare those.

COMPARE bool compare (const Student_info& x, const Student_info& y) { return x.name < y.name; } // uses the string comparison on the name fields of the two structures.

DECLARATIONS " same as what we called prototypes in C. " I'll probably continue to call them prototypes. " The last part of chapter 4 gives another discussion of separate compilation, header files, and pre-processor directives (#ifndef, #define, and #endif).