Escolar Documentos
Profissional Documentos
Cultura Documentos
* File: 20160909_CPP_L02.cpp.
* Title: 2016-09-09 C++ Lesson 02
* Author: Renato Montes
* Description: Notes for Albert Wei's C++ class
*/
/* Output */
cout << m
|--------|
//this (|-----|) returns cout, so you can keep on inserting with <<
ostream& operator<<(int)
//note that it returns a reference
//ostream objects cannot be copied anyway: cannot make a copy of
// standard output
/* Formatted Output */
/* * Using methods */
int n = 1;
cout << n << endl;//1
cout.setf(ios_base::showpos);
//now we print n:
cout << n << endl;//+1
cout << m << endl;//+2
cout.unsetf(ios_base::showpos);
cout << n << endl;//1
//this works for things that are ON or OFF
//there are things that aren't on or off
//some enim cases are more complicated
int n = 123;
cout << n << endl;//123
cout.setf(ios_base::hex, ios_base::basefield);
cout << n << endl;//76 or something
cout.setf(ios_base::dec, ios_base::basefield);
//you never call unsetf
//ios_base::basefield is sometimes called the "group name"
//hex and dec is part of a group with octo
//the group name unsets all three, and then sets hex on
//if two are on there's undefined behaviour!
/* * Using Manipulators */
int n=1, m=2;
cout << n << endl //1
<< showpos //manipulator
//good thing about manipulators: you use the same << operator
<< n << endl //+1
<< m << endl //+2
<< noshowpos
<< n << endl; //1
int n=123;
cout << n << endl //123
//now we simply insert the hex manipulator:
<< hex << n << endl //76
<< dec << n << endl; //123
//some manipulators:
//integer types: dec/hex/oct
//also for integers: showbase/noshowbase
//showbase is 0x76, where 0x is hex
//noshowbase would just be 76
//numbers: showpos/noshowpos
//namely showpos is +123, noshowpos is 123
//Note that setw only sets the width of the next formatted output
//applies to the next thing that is printed
//then the minimum field width reverts back to 0