Detailed input and output related classes and objects _c language in C + + programming

Source: Internet
Author: User

The concept of C + + input and output
We often use the input and output, are the end of the object, that is, from the keyboard input data, running results output to the monitor screen. From the operating system's point of view, each input connected to the host is treated as a file. In addition to the terminal for the object input and output, but also often disk (CD-ROM) as input output objects, disk files can either as input files, or as output files.

The input of a program refers to the transfer of data from the input file to the program, and the output of the program is the transfer of data from the program to the output file.

C + + input and output contains the following three aspects:
Input and output to the standard equipment specified by the system. That is, input data from the keyboard and output to the monitor screen. This input output is called standard input and output, referred to as standard I/O.
Input and output of the external disk file for the object, that is, input data from the disk file, data output to the disk file. The input output of the object is called the input and output of the file, referred to as file I/O.
Input and output the space specified in memory. You typically specify an array of characters as storage space (you can actually use that space to store any information). This input and output is called string input and output, referred to as series I/O.

C + + adopts different methods to achieve the above input and output. In order to realize the effective flow of data, C + + system provides a large I/O class library, calling different classes to achieve different functions.

In C language, using printf and scanf for input and output, often can not guarantee the input and output of the data is reliable and safe. In C + + input and output, the compiler system for the data type of strict inspection, all types of incorrect data can not be compiled. Therefore, C + + I/O operations are type safe. C + + I/O operations are extensible and can be used not only to input data of the standard type of output, but also to data from user-defined types. C + + Applies the same method to the standard type of data and to the input and output of the user declaring type data. C + + through the I/O class library to achieve rich I/O capabilities. C + + Input output is better than printf and scanf in C language, but more complex, to master a lot of details.
C + + I/O development-type safety and scalability

In c language, the output of PRIMF and scanf can not guarantee the data of input and output is reliable and safe. Readers who have learned C can analyze the following usage and want to output an integer with a format character of%d, but accidentally output a single-precision variable and a string with it, what happens? Assume that the system int used is two bytes.

  printf ("%d", I); I for integer variables, correct, output I of the value
  printf ("%d", f);//F is a single precision variable, output f variables in the first two bytes of content
  printf ("%d", "C + +");/Output Woo string "C + +" address

The compiler thinks that the above statements are all legitimate, and do not check the legality of the data type, obviously the result is not what people expect, when using scanf input, sometimes the problem is very covert. Such as:

  scanf ("%d", &i); Correct, enter an integer, assign to integer variable i
  scanf ("%d", i);/write &

If there is a declaration statement "int i = 1;" , define I as an integer variable with an initial value of 1. The compiler does not consider the above scanf statement to be an error, but instead stores the value of the loser in the memory unit with address 000001, which can have serious consequences.

C + + for compatibility with, the method of outputting and losing people with printf and scanf is reserved so that the large number of C programs written in the past can still run in the environment of C + +, but I hope readers will not use C's input and output mechanism when writing new C + + programs, but use C + + Their own unique output method of transmission. In C + + input and output, the compiler system for the data type of strict inspection, all types of incorrect data can not be compiled. Therefore, C + + I/O operations are type safe.

In addition, you can output and enter standard types of data (such as int, float, double, char) with printf and scanf, but you cannot output data for types (such as arrays, structs, classes) that the user declares. In C + +, the output of a class object is often encountered, and it is obvious that you cannot use printf and scanf to handle it. C + + I/O operations are extensible and can be used not only to output standard types of data, but also for user-defined types of data. C + + Applies the same method to the standard type of data and the output of the user declaring type data. Obviously, after the user declares a new class, it is impossible to directly output and lose objects of this class with printf and scanf functions.

Scalability is one of the important characteristics of C + + output, it can improve the reusability of software and speed up the software development process.

C + + through the I/O class library to achieve rich I/O capabilities. This makes C + + output significantly better than the C language of printf and scanf, but also pay the price, C + + I/O system becomes more complex, to master a lot of details. In this chapter, we can only introduce the basic concepts and basic operations, and some specific details can be further mastered in practical application in the future.

Classes and objects related to C + + input and output
Input and output are data transfer processes, and data flows from one place to another as water. C + + vividly calls this process a stream. C + + input-output stream is a sequence of several bytes of the Yu-section, the data in the order from one object to another object. The flow represents the flow of information from the source to the destination. In the input operation, the byte stream flows from the input device (such as the keyboard, disk) to the memory, and in the output operation, the byte stream flows from memory to the output device (such as screen, printer, disk, etc.). The contents of the stream can be ASCII characters, binary data, graphic images, digital audio videos, or other forms of information.

In fact, a memory buffer is opened in memory for each data stream to hold the data in the stream. When outputting data to the monitor with the cout and insert operator "<<", the data is sent to the output buffer in the program until the buffer is full or the Endl is encountered, and all the data in the buffer is sent to the display. When you enter, the data entered from the keyboard is placed first in the keyboard's buffer, and when you press ENTER, the data in the keyboard buffer enters the input buffer in the program, forms the cin stream, and then extracts the data from the input buffer with the extract operator ">>" to the relevant variable in the program. In short, the stream corresponds to the memory buffer, or the data in the buffer is the stream.

In C + +, the input output stream is defined as a class. A class in the I/O Library of C + + is called a streaming class (Stream class). An object defined with a stream class is called a Stream object.

In fact, cout and CIN are not the statements provided in the C + + language, they are objects of the iostream class, and they are referred to as cout and CIN statements when they are not aware of classes and objects, and are convenient for narration without causing misunderstandings. Just as C + + does not provide an assignment statement, only an assignment expression, a semicolon after the assignment expression is a C + + statement, for convenience, we are accustomed to call the assignment statement. Another example, in C language commonly used in printf and scanf for output and input, printf and scanf is the C language library functions in the input and output functions, generally also used to the printf and scanf functions composed of statements called printf statements and scanf statements. When using them, they should have an accurate understanding of their original concepts.

After understanding the classes and objects, we should have a deeper understanding of the input and output of C + +.

C + + compilation system provides a iostream class library for input and output. Iostream This word is composed of 3 parts, that is, the i-o-stream, meaning the input and output stream. The Iostream class library contains a number of classes for input and output. See table in common use


iOS is an abstract base class that derives from the IStream class and the Ostream class, and the 1th letter I and O of the two class names represent inputs (input) and outputs (output) respectively. The IStream class supports input operations, ostream classes support output operations, and iostream classes support input and output operations. The Iostream class is a class that derives from IStream classes and Ostream classes through multiple inheritance.

C + + for file input and output need to use the Ifstrcam and Ofstream class, two class names of the 1th letter I and O respectively represent input and output, the 2nd letter F represents the file. Ifstream supports input operations on files, Ofstream supports output operations on files. Class Ifstream inherits Class IStream, Class Ofstream inherits Class Ostream, Class FStream inherits class Iostream. See figure

There are other classes in the I/O class library, but for the average user, these are enough to meet the needs. If you want to learn more about the content and use of the class library, refer to the library manual for the C + + system you are using.
Header files related to the Iostream class library

The declaration of different classes in the Iostream class library is placed in a different header file, and the user uses the #include command in their own program to include the relevant header file, which is equivalent to declaring the required class in this program. Can change-kind of saying: the header file is the interface between the program and the class library, the interface of the Iostream class library is implemented by different header files respectively. Commonly used to have
Iostream contains the basic information needed to manipulate an input-output stream.
FStream I/O operations for files that are used by user management.
Strstream is used for string stream I/O.
Stdiostream is used to mix C and C + + I/o mechanisms, such as converting a C program into a C + + program.
Iomanip should include this header file when using formatted I/O.
Stream objects defined in the iostream header file

The classes defined in the iostream header file are Ios,istream,ostream,iostream,istream _withassign, ostream_withassign,iostream_withassign, and so on.

Iostream.h contains the basic information needed to manipulate an input-output stream. Therefore, most C + + programs include iostream.h. The iostream.h header file defines not only the related classes, but also 4 kinds of stream objects, as shown in the table

Define the above 4 stream objects in the iostream header file in the following form (take cout as an example):

  Ostream cout (stdout);


When defining cout as a Ostream Stream class object, the standard output device stdout is used as a parameter so that it is associated with a standard output device (monitor) if

  cout <<3;


Will output 3 on the monitor's screen.
Overloading an operator in a iostream header file

"<<" and ">>" were originally defined as the left and right shift operators in C + +, because they were overloaded in the iostream header file so that they could be used as input and output operators for standard type data. Therefore, the iostream must be included in the program with the #include command in the program in which they are used.

  #include <iostream>

In the IStream and Ostream classes (all two classes are declared in iostream), there is a set of member functions that overload the displacement operator "<<" and ">>" so that it can be used to enter or output data of various standard data types. For different standard data types to be overloaded separately, such as:

  Ostream operator << (IM); Used to insert an int data to the output stream
  ostream operator << (float);//To insert a float data
  ostream operator << (char) to the output stream; Used to insert a char data into the output stream
  ostream operator << (char *);//For inserting a string data into the output stream


Wait If you have the following expression in your program:

  cout<< "C + +";


Actually corresponds to:

  Cout.operator << ("C + +")

The value of "C + +" is its first byte address, which is the type of character pointer (char *), so select the last operator overload function above, insert the string into the cout stream by overloading the function body of the function, and the function returns the Stream object cout.

The operator ">>" has been overloaded in the IStream class as an extraction operator for the following standard types: char, signed char, unsigned char, short, unsigned short, int, unsigned Int,lon G, unsigned long, float, double, long double, char *, signed char *, unsigned char * etc.

In the Ostream class, "<<" is overloaded with the insert operator, and the applicable type adds a void * type, in addition to the above standard type.

If you want to use "<<" and ">>" for the type of data you declare, you cannot simply use the containing iostream header file, and you must overload "<<" and ">>" yourself.

How do you understand the role of operator "<<" and ">>"? There is a simple but vivid approach: they point to the direction in which data is moved, such as

  >>a


The arrow direction indicates that the data is placed in person a. and

  <<a


The arrow direction indicates that the data is taken from a.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.