Library Functions
Mar 18, 2016
Library Functions
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}
CSCE 106 3
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
CSCE 106 5
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 ;
CSCE 106 7
Input/Output Manipulators
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
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
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
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”;
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
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
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)
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);
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;
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
CSCE 106 18
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
CSCE 106 20
Next lecture will be about Selection/Decision in C++