Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects.

Similar presentations


Presentation on theme: "Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects."— Presentation transcript:

1 Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 1 2-A. Using Objects of Class Type

2 Formal specifications for: 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 2 Procedural: Operations on Data Objects a 5 b 10 c 15 + (i) actions and type of data int -- indicates operation on integer (ii) variable with unique value assigned in memory (iii) applying actions to data c=a+b

3 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 3 Object Oriented: operations of/on objects and interactions between objects (ii) defining objects (iii) applying actions on objects Formal specifications for: (i) bundling actions and data today.advance() today.tellMo() today.tellDay() today.tellYear() Date attributes/data: behavior: advance() tellMo() tellDay() tellYear() month day year today attributes/data: behavior: advance() tellMo() tellDay() tellYear() month 9 day 8 year 2008

4 6/30/2015 MET CS 563--Fall 2008 2-A. Using Class Type Objects 4 Procedural  Object Oriented (ii) objects today.advance() today.tellMo() today.tellDay() today.tellYear() (iii) actions on objects (i) type Date attributes/data: behavior: advance() tellMo() tellDay() tellYear() a 5 b 10 c 15 int c=a+b month day year today attributes/data: behavior: advance() tellMo() tellDay() tellYear() month 9 day 8 year 2008

5 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 5 string Class (ii) defining objects(iii) applying actions on objects (i) bundling actions and data name1.length() name1.substr(0,1) name1 + name 2 cout << name1 cin >> name1 getline(cin, sentence) string data: behavior: length() substr(start,length) + >> << name1 data: behavior: length() substr() + >> << position01… char 0123 Mary dot operator on member functions global function getline(in, string)

6 #include contains the definitions of the string class (data and functions) string name; //declaration string name1 = "Mary"; //definition string name2 = "Emily Dickinson"; cin >> name; //input placed in name cout << name1; //outputs Mary 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 6 string Objects—declare, define, i/o

7 string s1 = "Henry"; string s1 = "David"; string s1 = "Thoreau"; cout << s1 + s2 + s3; //"HenryDavidThoreau" string name = s1 + " " + s2 + " " + s3; cout << name; //"Henry David Thoreau" 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 7 string Objects—operations

8 The input and output streams cin and cout are in fact predifined objects of iostream classes. In this they are similar to string objects name, sentence, etc. Output streams come with a set of special functions, called manipulators, that allow changing the appearance of the output, such as setw : sets the number of characters or width of the output field fixed : prints floating point numbers with trailing zeros scientific: prints floating point numbers in scientific notation setprecision : number of digits after decimal point when used with fixed or scientific left and right alignment (See o program fromattedIO.cpp for examples o specifications in documentation for more detail on manipulator use ) 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 8 Formatted Output--Manipulators

9 cout << manipulator << expression | identifier; Older notation cout << setiosflags(ios :: manipulator) << expression | identifier; Examples: fromatted_IO.cpp 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 9 IO Manipulators--Syntax

10 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 10 I/O Manipulators--Exmpales setprecision( ) sets the number of digits, or precision, to the right of the decimal point to integer given in the parentheses. Examples: setprecision(3) will print 3.333 setprecision(4) will print in the form 4.4444 setiosflags( | |… ) Examples: setiosflags(ios::fixed) prints floating point numbers with decimal point and without exponent, in so called fixed-point formal (as opposed to scientific with exponent) setiosflags(ios::showpoint) prints decimal point and trailing zeros even if amount is an integer, e.g. 77.00 instead of 77

11 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 11 #include using namespace std; int main() { double x1= 1.34, x2 = 4.63; double y1 = 27.563, y2 = 72.02; double z1 = 759.12345, z2 = 049.22; //no formatting--default prints left justified w/o spaces cout << "No Formatting: " << endl; cout << x1 << x2 << endl; cout << y1 << y2 << endl; cout << z1 << z2 << endl <<endl; string: def and io No Formatting: 1.344.63 27.56372.02 759.12349.22

12 string: setw(n) 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 12 cout << "Using setw(20)" cout << "must be set before each output item" << endl; cout << setw(20) << x1 << setw(20) << x2 << endl; cout << setw(20) << y1 << setw(20) << y2 << endl; cout << setw(20) << z1 << setw(20) << z2 << endl <<endl; Using setw(20)--must be set before each output item 1.34 4.63 27.563 72.02 759.123 49.22

13 string: fixed 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 13 cout << "Using fixed: " cout << "default precision of " cout << "6 decimal digits after point printed " << endl; cout << "Once set remains in force till changed " << endl; cout << fixed; cout << setw(20) << x1 << setw(20) << x2 << endl; cout << setw(20) << y1 << setw(20) << y2 << endl; cout << setw(20) << z1 << setw(20) << z2 << endl <<endl; Using fixed: default precision of 6 decimal digits after point printed Once set remains in force till changed 1.340000 4.630000 27.563000 72.020000 759.123450 49.220000

14 string: fixed and setprecision 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects 14 cout << fixed << setprecision(2); cout << setw(20) << x1 << setw(20) << x2 << endl; cout << setw(20) << y1 << setw(20) << y2 << endl; cout << setw(20) << z1 << setw(20) << z2 << endl <<endl; 1.34 4.63 27.56 72.02 759.12 49.22


Download ppt "Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects."

Similar presentations


Ads by Google