[C++] I/O-Stream

04/19/2008 15:01 Opalis#1
==========Input-/ Output-Stream classes==========

C++ got a new input-/output-stream which is based on classes.They are made available in their own library which is called iostream-library.
The image shows the class-hierarchy.



The class ios is the basis-class of all stream-classes.
The class istream was designed for the Reading.
The class ostream was designed for Writing.
In these istream and ostream classes are the operators << and >> defined.
[Only registered and activated users can see links. Click Here To Register...]
Standard-Streams

  • cin : object is used for input ( istream )
  • cout : object is used for output ( ostream )
  • cerr : object is used for unbuffered error-outputs ( ostream )
  • clog : object is used for buffered error-outputs ( ostream )


==========Basics==========
Code:
#include <iostream>
using namespace std;

int main()
{
   cout << "Hello!I am a console application." << endl; 
   cout << "Please enter a number" << endl;
   int number;
   cin >> number;

   system("PAUSE");
   return 0
}
-console output is used for displaying the sentence Hello!I am a console application.
-the << and >> are the operators for output and input.
-endl is used for a line feed
-int number : numberr is declared as an integer.
-console input is used for the input.


==========Manipulator==========
Code:
// [b]showpos[/b] is used for displaying algebraic sign
cout << showpos << 123456789;   // output : + 123456789
//You can also use :
cout.setf( ios::showpos )
cout << 123456789;
//All following outputs positive numbers with algebraic sign
cout << 558;   // output : + 558


//Use [b]noshowpos[/b] to cancel the positive algebraic sign 
cout << noshowpos << 123456789   // output : 123456789
//You can also use :
cout.unsetf( ios::showpos )
cout << 123456789;
manipulators for formatting integers :
  • oct : octal notation
  • hex : hexadecimal notation
  • dec : decimal notation ( standard )
    -------------------------------------------------
  • showpos : positive integers are displayed with a algebraic sign
  • noshowpos : posituve integers are not displayed with a algebraic sign ( standard )
  • uppercase : hexadecimal notations are used with capital letters
  • nouppercase : hexadecimal notations are used without capital letters ( standard )

Example :

Code:
#include <iostream>
using namespace std;

int main()
{
   int number;
   cout << "Enter a number" << endl;
   cin >> number;
   
   cout << " octal : " << oct << number << endl;
   cout << " decimal : " << dec << number << endl;
   cout << " hexadecimal : " << hex << number << endl;

    system("PAUSE");
    return 0;
}
I gonna continue this tutorial later..Gonna play a little bit now :)

Opalis