Chapter 3
  1. cout

  2. cin

  3. named constants

  4. C++/C has the following standard arithmetic operators.
    1. + addition
    2. - subtraction
    3. * multiplication
    4. / division.
      Note there is only one division operator for both integer and floating point division.
    5. % modulus,
      computes the remainder from a division, this is defined for integer data types only.
    6. C does not have an operator for exponentiation.
      So in order to raise a value to a power you must use the pow function like:
      #include <math.h>
      float x,y,z;
      x=pow(y,z);
      
      This will place yz in x.


  5. C has two operators for incrementing and decrementing. (This usually means adding or subtracting one, except when we get to pointers).
    1. ++ increment
    2. -- decrement

      These operator must refer to a memory location also called an lvalue or lhs (i.e. an address) not an expression.

      These operators have a prefix and postfix notation. In postfix notation the variable is changed after its value has been accessed. In prefix notation the change happens before accessing the value of the variable.

  6. Bitwise operators are used to manipulate bits. The bitwise operators are:
    1. & and
       int a=10,b=12,c
      
        c=a&b;
                          a=  1010
                          b=  1100
                             -------
                          c=  1000
      
    2. | inclusive or
       int a=10,b=12,c
      
        c=a|b;
                          a=  1010
                          b=  1100
                             -------
                          c=  1110
      
    3. ^ exclusive or
       int a=10,b=12,c
      
        c=a^b;
                          a=  1010
                          b=  1100
                             -------
                          c=  0110
      
    4. ~ bitwise negation turn 0s to 1s and 1s to 0s.
    5. >> shift right
       int a=32,b;
      
       b=a>>3;
                  a= 0100000
                  b= 0000100
      
    6. << shift left
       int a=2,b;
      
       b=a<<3;
                  a= 0000010
                  b= 0010000
      


  7. C has the following assignment operators;
    1. = assignment,
      This operator is evaluated right to left so. a=b=c=2; is a valid c statement.
    2. C also has shorthand assignment operator so that a variable does not need to be specified twice in a expression.
      1. += add and assign a+=b means a=a+b
      2. -=
      3. *=
      4. /=
      5. %=
      6. >>=
      7. <<=
      8. &=
      9. |=
      10. ^=


  8. Pointer indirection also called the deference operator. The * means use the address in the pointer to access memory. For example:
    void main()
    {
     int a,*p;
     a=3;
     cout << a << " " << &a << endl;
     p=&a;
     *p=4;
     cout << *p << " " << &p << " " << << p << endl;
    }
    
    3 4160748428
    4 4160748424 4160748428
    
  9. iostream
    Working class derived from both istream and ostream. you include this class with <iostream> definition.

    1. Objects
      1. cin
        Standard input of class istream.
      2. cout
        Standard output of class ostream.
      3. cerr and clog
        Standard error output of class ostream.

    2. Overloaded Operators
      1. <<
        Shift left is overload and called the stream insertion operator. This operator was chosen because it associates left to right, low order of precedence and just the way it look denoting the flow of data. Which means that statements like:
        	int i=47;
        	cout << i << " " << i + 3 << endl;
         
        Gives the correct output of:
        47 50
        
        This is accomplished thru the use of multiple overloaded operator definitions.

        There are defined manipulators for cout like:

        • endl -print an end of line the same as sending '\n' to cout.
        • flush - flush the output stream buffer.
      2. >>
        The shift right has been overloaded for the istream class and called the stream extractor.

    3. Common manipulators and member functions to use these include <iomanip>.

      manipulatorMember functionDescription
      decsetf(ios::dec)Set radix to 10
      hexsetf(ios::hex)Set radix to 16
      octsetf(ios::oct)Set radix to 8
      setfill(c)fill(c)Set the fill character to c.
      setprecision(n)precision(n)Set display precsion to n
      setw(n)width(n)Set output field width to n.
      setiosflags(p)setf(p)Turn on IOS icontrol bits
      resetiosflags(p)unsetf(p)Turn off IOS control bits

    4. Format state flag bits

      IOS::FlagDescription
      skipws Skip whitespace characters on an input stream
      left Left justify ouput
      right Right justify the output
      internal Left justify the sign, right justify the number
      dec Display the number in base 10
      oct Display the number in base 8
      hex Display the number in base 16
      showbase Specify base ahead of the number
      showpoint Display floating point numbers with a decimal point
      uppercase Use upper case in displaying numbers
      showpos Show positive or negative signs
      scientific Output floating point in scientific notation
      fixed Specify the number of output to right of decimal point

    5. Examples


      1. //
        //
        //
        #include <iostream>
        #include <iomanip>
        using namespace std;
        void main()
        {
            long ioflags;
            ioflags=cout.flags();
        
            int i=47;
            cout << "Integer " << i << endl;
        
            char a='a';
            cout << "Character output " << a << endl;
        
            float f=3.14159;
            cout << "Floating Point " << f << endl;
            
            cout << "Integer in hex  "  << hex << i << endl;
            cout << "Integer in Octal " << oct << i << endl;
            cout << dec;
            
            cout << "Integer in width " << setw(5) << i << endl;
            cout << "Integer in width with fill " << setfill('-') << setw(5) << i << endl;
            
            cout.flags(ioflags);
            cout.setf(ios::left);
            cout << "Justify left " << setw(5) << i << endl;
            
            cout.flags(ioflags);
            cout.fill('-');
            cout.setf(ios::right);
            cout << "Justify Right " << setw(5) << i << endl;
            
            i=-47;
            cout.fill(' ');
            cout.flags(ioflags);
            cout.setf(ios::internal);
            cout << "Internal " << setw(5) << i << endl;
            }
        

        Integer 47
        Character output a
        Floating Point 3.14159
        Integer in hex  2f
        Integer in Octal 57
        Integer in width    47
        Integer in width with fill ---47
        Justify left 47---
        Justify Right ---47
        Internal   -47
        

      2. #include <iomanip>
        #include <iostream>
        using namespace std;
        main()
        {
          char a='a';
          int  i=34,j=-34;
          float f=3.14159;
          
          cout << a << endl;
          cout << (int)a << endl;
          cout << i << " " << j << endl;
          cout << (unsigned)i << " " << (unsigned)j << endl;
          cout << oct << i << endl;
          cout << hex << i << endl;
          cout << dec;
        
          cout << f << endl;
        
          cout << setw(3) << i << " " << setw(3) << j << endl;
          cout << setiosflags( ios:: showpoint | ios::fixed);
          cout << setw(6) << setprecision(2) << f << endl;
        
           i=8;
           j=3;
           cout << setw(i) << setprecision(j) << f << endl;
          return(0);
        }
        

        a
        97
        34 -34
        34 4294967262
        42
        22
        3.14159
         34 -34
          3.14
           3.142
        

      3. #include <iostream>
        using namespace std;
        main()
        { 
          char a[40];
          int  i,j;
          float f;
         
          cin  >> a ;
          cin >> i >> j;
          cin  >> f;
          cout << a << endl;
          cout <<i << " " << j  << endl;
          cout << f << endl;
          return(0);
        }
        

        --Input file
        hello
        3 4 
        3.4 
        --- Run the program
        hello
        3 4
        3.4
        

  10. File I/O
    //
    // File test
    //
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void main()
    {
    	int x =3;
    	float y=3.14159;
    
    	ofstream out;
    
    	out.open("testfile.txt");
    	out << x << endl;
    	out << y << endl;
    	out.close();
    
    	ifstream in;
    
    	in.open("testfile.txt");
    
    	in >> x;
    	cout << x << endl;
    	in >>y;
    	cout << y <<endl;
    
    	in.close();
    }