typeName variableName;An object declaration is really the same thing, because a class IS a type. Format:
className objectName;Example: if we have a class called Circle, then we can declare:
Circle c1; Circle myCircle; Circle c2, c3, c4, c5;
objectName.functionCallExample: If class Circle has member functions Draw and SetRadius, we might make calls like this:
c1.Draw(); // draw the Circle object c1 myCircle.SetRadius(5); // set the radius of myCircle to 5
ostream cout; istream cin;
int x = 1234; cout.setf(ios::right); cout.width(10); cout << "Hello"; cout.width(15); cout << x; // output of the above is: // Hello 1234
int x = 1234; cout.setf(ios::right); cout.fill('.'); // change the fill character cout.width(10); // set field width to 10 cout << x; // print x // output of the above is: // ......1234
cout << endl; // endl is a stream manipulator
cout << setw(10); // setw() is a parameterized manipulator
#include <iomanip>
cout << setw(10) << "Hello" << endl;
cout.precision(2); // sets decimal precision to 2 significant digits cout << setprecision(2); // does the same thing!
cout.width(10); // sets field width to 10 for next output cout << setw(10); // does the same thing!
cout.fill('*'); // sets fill character to '*' cout << setfill('*'); // does the same thing!
cout.setf(ios::left); // sets left justification flag cout << setiosflags(ios::left); // does the same thing!
cout.setf(ios::left); // sets left justification for cout cout << left; // also sets left justification for coutCaution: Some of these manipulators that correspond to formatting flags were introduced in a newer version of the <iomanip> library, just a few years ago. Some older compilers (still in use) may not recognize them!
Flag Name | Corresponding Stream Manipulator |
Description |
---|---|---|
ios::fixed | fixed | if this is set, floating point numbers are printed in fixed-point notation. When this flag is set, ios::scientific is automatically unset |
ios::scientific | scientific | if this is set, floating point numbers are printed in scientific (exponential) notation. When this flag is set, ios::fixed is automatically unset |
ios::showpoint | showpoint | if this is set, the decimal point is always shown, even if there is no precision after the decimal. Can be unset with the manipulator noshowpoint |
ios::showpos | showpos | if set, positive values will be preceded by a plus sign + . Can be unset with the manipulator noshowpos. |
ios::right | right | if this is set, output items will be right-justified within the field (when using width() or setw()), and the unused spaces filled with the fill character (the space, by default). |
ios::left | left | if this is set, output items will be left-justified within the field (when using width() or setw()), and the unused spaces filled with the fill character (the space, by default). |
ios::showbase | showbase | Specifies that the base of an integer be indicated on the output. Decimal numbers have no prefix. Octal numbers (base 8) are prefixed with a leading 0. Hexadecimal numbers (base 16) are prefixed with a leading 0x. This setting can be reset with the manipulator noshowbase. |
ios::uppercase | uppercase | specifies that the letters in hex outputs (a-f) and the letter 'e' in scientific notation will be output in uppercase. This can be reset with the manipulator nouppercase. |
Here is a table of other common stream manipulators, all from
namespace std
Manipulator | Description |
---|---|
flush | causes the output buffer to be flushed to the output device before processing proceeds |
endl | prints a newline and flushes the output buffer |
dec | causes integers to be printed in decimal (base 10) |
oct | causes integers from this point to be printed in octal (base 8) |
hex | causes integers from this point to be printed in hexadecimal (base 16) |
setbase() | a parameterized manipulator that takes either 10, 8, or 16 as a parameter, and causes integers to be printed in that base. setbase(16) would do the same thing as hex, for example |
internal | if this is set, a number's sign will be left-justified and the number's magnitude will be right-justified in a field (and the fill character pads the space in between). Only one of right, left, and internal can be set at a time. |
boolalpha | causes values of type bool to be displayed as words (true or false) |
noboolalpha | causes values of type bool to be displayed as the integer values 0 (for false) or 1 (for true) |