library functions
DESCRIPTION
Library Functions. 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}. Math Functions. Math functions are contained in the header file/class cmath - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/1.jpg)
Library Functions
![Page 2: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/2.jpg)
CSCE 106 2
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}
![Page 3: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/3.jpg)
CSCE 106 3
![Page 4: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/4.jpg)
CSCE 106 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 = 5.7 + sqrt(x);z = sqrt(x+w);
pow(x,y) x = 2.0;z = 3;y = pow(x,z);
function call
function name
arguments
![Page 5: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/5.jpg)
CSCE 106 5
![Page 6: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/6.jpg)
CSCE 106 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 ;
![Page 7: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/7.jpg)
CSCE 106 7
Input/Output Manipulators
![Page 8: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/8.jpg)
CSCE 106 8
ExerciseWrite 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. nrCF 1
![Page 9: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/9.jpg)
CSCE 106 9
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
![Page 10: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/10.jpg)
CSCE 106 10
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
![Page 11: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/11.jpg)
CSCE 106 11
“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 <string> Needed for using string identifiers/objects,
but not needed for literalsstring instructor, student = “Omar”;
![Page 12: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/12.jpg)
CSCE 106 12
“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
![Page 13: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/13.jpg)
CSCE 106 13
“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
![Page 14: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/14.jpg)
CSCE 106 14
“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)
![Page 15: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/15.jpg)
CSCE 106 15
“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);
![Page 16: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/16.jpg)
CSCE 106 16
“string” Class (cont’d) Inserting characters into a string
cout << student.insert(0, “Mr. ”) << endl;
Replacing portion of a stringcout << student.replace(14, 3, “edy”) << endl;
Deleting portion of a string cout << student.erase(3, 1) << endl;
![Page 17: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/17.jpg)
CSCE 106 17
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
![Page 18: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/18.jpg)
CSCE 106 18
![Page 19: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/19.jpg)
CSCE 106 19
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
![Page 20: Library Functions](https://reader033.vdocuments.mx/reader033/viewer/2022052403/568144d4550346895db1a0ae/html5/thumbnails/20.jpg)
CSCE 106 20
Next lecture will be about Selection/Decision in C++