I/O and Program Control Statements Dick Steflik
Overloading • C++ provides two types of overloading • function overloading • the ability to use the same function name to have multiple definitions • operator overloading • the ability to define multiple definitions for any (almost) of the graphical symbols used for operators (+,-,*,<<,>>…)
But First... I/O • To enable your program to do input and output you must include the IOSTREAM header file • if you are using namespace std • #include <iostream> • if you are not using a namespace • #include <iostream.h> • or tell the compiler explicitly • std::cout << ……...
more on I/O... • Once iostream has been included we can use the predefined objects cout, cin and cerr. • cout, cin and cerr are predefined objects of class iostream; they are not I/O statements in the sense of program statements, they are objects that we will invoke methods on.
more on I/O... • The class iostream allows us to define something called a stream, a stream is nothing more than a sequence of bytes • in the case of cin it is the sequence of bytes coming from sysin (the keyboard) • in the case of cout it is the sequence of bytes being transferred to sysout (the display)
the insertion operator <<... • For iostream the “shift left” operator << has been overloaded and is called insertion • << is used to insert (write) characters into the stream being used • cout << “Dick Steflik”; • inserts “Dick Steflik” into the console output stream • cout << “your grade is : “ << grade;
the extraction operator >>... • For iostream the “shift right” operator >> has been overloaded and is called extraction • >> is used to extract (read) information from the console input stream (keyboard) and assign it to program variables • int x ; cin >> x; • read an integer from the input stream into the variable x
iomanip.h • iomanip.h provides a number of stream manipulators that when placed inline with our cin/cout method calles affect the way the commands are handled • setbase(n) will set the base to be used for insertion and extraction to n • cout << setbase(8) << n; // will cause n to be printed out as an octal number • cout << oct << n; // will accomplish the same thing • cout << hex << n; // will print n as a hexadecimal number • cout <<dec << n; // will print n as a decimal number • setw or width - will set the field width of subsequent fields • cout .setw(10); cout<< n; // n will be printed right justified in a field 10 char wide • cout << setw(12) << n; // same thing
Formatting flags • setf, unsetf and flags control I/O flag settings • ios::skipws - skip white space on input stream • ios::left - left justify output in a field, pad to right (if needed) • ios::right - right justify in a field, pad on left (if needed) • ios::internal - left justify the sign, right justify value • ios::dec - treat integers as base 10 • ios::oct - treat integers as octal • ios::hex - treat integers as hexadecimal • ios::showbase - display the base ahead of number (0 for octal, 0x for hex) • ios::showpoint - display floating point numbers with a decimal point • ios::fixed - display floating point numbers with specified number of decimal digits • ios::uppercase - show upper case (ABCDEF) letters in hexadecimal numbers • ios::scientific - show floating point numbers in scientific notation
Using Formatting flags • The flags member function must specify a value for each formatting flag (seldom used) • The setf member function and the stream manipulator setiosflags are used to set one or more formating flags; likewise unsetf and resetioflags will reset one or more formatting flags. • cout << setf( ios::hex | ios::uppercase) << x; • displays x in hexadecinal format with uppercase letters for letters
Deitel & Deitel • Review Chapter 11 of the Deitel & Deitel C++ How to Program book for a good discussion on Stream I/O