Download presentation
Presentation is loading. Please wait.
Published byGodwin McCarthy Modified over 8 years ago
1
Library Functions
2
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}
3
CSCE 1063
4
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
5
CSCE 1065
6
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 ;
7
CSCE 1067 Input/Output Manipulators
8
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.
9
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
10
CSCE 10610 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
11
CSCE 10611 “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”;
12
CSCE 10612 “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
13
CSCE 10613 “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
14
CSCE 10614 “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)
15
CSCE 10615 “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);
16
CSCE 10616 “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;
17
CSCE 10617 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
18
CSCE 10618
19
CSCE 10619 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
20
CSCE 10620 Next lecture will be about Selection/Decision in C++
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.