Presentation is loading. Please wait.

Presentation is loading. Please wait.

I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.

Similar presentations


Presentation on theme: "I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3."— Presentation transcript:

1 I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3

2 I/O and Data Formatting A stream is a flow of characters either “in” or “out” of your program. A stream is a flow of characters either “in” or “out” of your program. C++ relies on libraries for input/output related to the console, file, or other devices. C++ relies on libraries for input/output related to the console, file, or other devices.

3 Input Stream Input stream is the input being fed into the program –uses cin >> is the extraction operator –Program waits for input and a (after all input) to designate that the input is complete –Spaces or on input will separate the data cin >> identifier1 >> identifier2; cin >> identifier1 >> identifier2;

4 Output Stream Output stream is the stream of output generated by the program –uses cout << is the insertion operator –Can include: arithmetic expressions “\n” or endl to indicate a new line quoted strings cout << num1 << “number 1 \n”; cout << num1 << “number 1 \n”;

5 Sample cout statements cout << num1; cout << num1; cout << num1 << “ number of pencils \n”; cout << num1 << “ number of pencils \n”; cout << num1 * pr << “ price of pencils \n”; cout << num1 * pr << “ price of pencils \n”; cout << num1 << “ pencils “ << pr << “ price “ cout << num1 << “ pencils “ << pr << “ price “ << num1 * pr << “ price of pencils \n”; << num1 * pr << “ price of pencils \n”; cout << “\n”; cout << “\n”; cout << endl; cout << endl;

6 Formatting The same data can be displayed in many different ways: 320320.0320.00000 320 3203.2e+2+320.0

7 Formatting Format Flags setf(ios:: ) setf(ios:: ) common flagnames: common flagnames: fixed fixed left left showpoint showpoint right right (Note: ios is a class from the stream library) (Note: ios is a class from the stream library)

8 Formatting Flags fixed - ensures fixed format, i.e. no scientific notation showpoint - ensures that decimal points and trailing zeros are displayed left adjusts output right adjusts output

9 Examples: cout.setf(ios::fixed); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.setf(ios::showpoint); cout.setf(ios::fixed | ios::showpoint); cout.setf(ios::fixed | ios::showpoint);

10 Formatting Manipulator Manipulators are placed after the insertion operator; the arrow notation << is called the insertion operator. setprecision (2); - two decimal digits are output setprecision (2); - two decimal digits are output setw (4); - describes width of next field setw (4); - describes width of next field cout.width (4); - describes width of next field cout.width (4); - describes width of next field Note: #include is required Note: #include is required

11 Examples cout << setprecision (2) << “Value is: ” << 10.5; cout << setprecision (2) << “Value is: ” << 10.5; cout << setw(7) << value1 << setw(5) << value2 << endl; cout << setw(7) << value1 << setw(5) << value2 << endl; cout << setw(4) << “abc”; // Note: only for next variable cout << setw(4) << “abc”; // Note: only for next variable cout.precision(4); cout.precision(4); cout.width(10); // Note: only for next variable cout.width(10); // Note: only for next variable

12 Formatting Rules 1 Flags and most manipulators remain set until reset or overridden with a new flag or manipulator 2 When a field is too small, C++ will adjust it and print the true value of a number 3 When a string variable is output, the field width will be filled with whatever is in memory unless a ‘\0’ is found. ‘\0’ is referred to as the NULL character and is a special ‘\0’ is referred to as the NULL character and is a special character similar to ‘\n’ character similar to ‘\n’ 4 Extra bytes are padded with spaces

13 Objects Objects are all around us. Computer programs attempt to mimic the objects in the world. An object is an instance of a class. Example: person class

14 Objects An object has structure: –Attributes (properties) –Behaviors or operations it can carry out

15 Class A Class is a template for making objects (cookie cutter) –it becomes a data type definition like int or char –a type whose variables are objects member functions (behaviors) holds data values (attributes)

16 Class Definition

17 Abstract Data Types Guidelines Make all member variables private members of the class.

18 Abstract Data Types Guidelines Make each of the basic operations that the programmer needs a public member function of the class, and fully specify how to use each public member function. –Interface: Class definition Function prototypes Comments

19 Abstract Data Types Guidelines Implementation consists of function definitions


Download ppt "I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3."

Similar presentations


Ads by Google