Library Functions. CSCE 1062 Outline  cmath class library functions {section 3.2}  iomanip class library functions {section 8.5}  string class library.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall A. Using Class Type Objects.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Chapter 3: Input/Output
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Due Dates Quiz 1, 2 : Friday September 11 th Quiz 3 : Friday September 18 th Quiz 4 : Monday September 28 th Lab 1, 2 and 3 : Friday Sep 11 th Project.
1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.
Input, Output, and Processing
Instructor - C. BoyleFall Semester
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
Expressions and Interactivity. 3.1 The cin Object.
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
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.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
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.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Topic 2 Input/Output.
C++ Basic Input and Output (I/O)
What Actions Do We Have Part 1
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Chapter 3: Expressions and Interactivity.
Standard Input/Output Streams
Expressions and Interactivity
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Chapter 3: Input/Output
Wednesday 09/23/13.
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Let’s all Repeat Together
C++ Programming Basics
Using string type variables
Introduction to cout / cin
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Programming Fundamental-1
Presentation transcript:

Library Functions

CSCE 1062 Outline  cmath class library functions {section 3.2}  iomanip class library functions {section 8.5}  string class library functions {sections 2.3 & 3.7}  Object Orientation (OO) {section 1.3}

CSCE 1063

4 Math Functions  Math functions are contained in the header file/class cmath  Examples of some predefined math functions are:  sqrt(x) x = 16.0; y = sqrt(x); z = sqrt(x); z = sqrt(x+w);  pow(x,y) x = 2.0; z = 3; y = pow(x,z); function call function name arguments

CSCE 1065

6 “iomanip” Class  A C++ library containing manipulators to control format of output.  setw(n)  n is int.  controls the width of the following output field only.  the default width is 0.  cout << setw(4) << x ;  setprecision(n)  n is int.  sets the precision to n decimal places.  it remains in effect for future output (until changed)  the default is 6 decimal places.  cout << setprecision(4) << x ;

CSCE 1067 Input/Output Manipulators

CSCE 1068 Exercise Write a complete C++ program to calculate, and output the future value (F), rounded to the nearest piaster, of a single cash flow (C) invested in a bank for (n) years at a nominal interest rate (r). F is calculated from the following formula: where C, r, and n are input by the user.

CSCE 1069 Solution Steps  Problem statement/Requirements phase. Already done, as problem is simple  Analysis phase.  Problem input float C, r, n  Problem output float F (rounded to 2 decimal places)  Additional program variables NA

CSCE Solution Steps (cont’d)  Processing formulas F = C * pow(1+r, n)  Design phase. Draw the flow chart.  Implementation phase. Transform your flow chart to a C++ program.  Testing phase.  Test results using various input combinations  Verify results by hand or with calculator

CSCE “string” Class  string literals are enclosed in double quotes, e.g.: “Enter miles: “ “ABC” “B” “true” “1234”  string as a data type is not built-in in C++, it comes from a predefined library  #include  Needed for using string identifiers/objects, but not needed for literals string instructor, student = “Omar”;

CSCE “string” Class (cont’d)  Common operations on string objects: > = + cin >> instructor; // reads up to blank or return cout << student << endl; (New lines: endl; or ‘\n’;)  + puts strings together (concatenation) student = student + “ “ + “Mohammad”; Note that we need a space between names

CSCE “string” Class (cont’d)  Can read string with blanks getline(cin, lastName, ‘\n’); // reads an entire line  Special/control characters ’\n’ newline ’\b’ backspace ’\t’ tab ‘\’’single quote ‘\”’double quote ‘\\’backslash

CSCE “string” Class (cont’d)  Attributes include:  character sequence it stores  length  Accessing String Operations:  Member functions length() and at()  These functions can be called using dot notation (syntax:object. function-call)  Applies the identified operation to the named object, e.g.: student. length( ), student. at(0)

CSCE “string” Class (cont’d) cout << student.length() << endl; cout << student.at(0) << student.at(5) << endl; cout << student.at(student.length() – 1) << endl;  Additional Member Functions:  Searching for a string cout << student.find(“am”) << endl;  Assign a substring to a string object instructor.assign(instructor,0,3);

CSCE “string” Class (cont’d)  Inserting characters into a string cout << student.insert(0, “Mr. ”) << endl;  Replacing portion of a string cout << student.replace(14, 3, “edy”) << endl;  Deleting portion of a string cout << student.erase(3, 1) << endl;

CSCE Object Orientation  C++ is an object oriented language  C++ promotes code reuse with predefined classes, that has functions, defined in the standard library  Classes extend C++  A class has data/attributes and functions/methods  An instance of a class is an object.  Classes are organized in a hierarchy  Super classes  Sub classes

CSCE 10618

CSCE Object Orientation (cont’d)  Important properties of OO:  Abstraction Extract the relevant properties of an object while ignoring inessential details  Encapsulation Breaking down an object into parts, hiding and protecting its essential information, and supplying an interface to modify the information in a controlled and useful manner

CSCE Next lecture will be about Selection/Decision in C++