example, the STL defines generic data structures, such as queues, stacks, and lists, which you can use in
You will also want to study the C+ function library. It contains a wide array of routines that will simplify
the creation of your prog
Here, type specifies the target type of the cast, and expr is the expression being cast into the new type.
It must be stressed that the use of const_cast to cast away const-ness is a potentially dangerous feature.
Use it with care.
One other point: Only c
Perhaps the most important of the additional casting operators is the dynamic_cast. The dynamic_cast
performs a runtime cast that verifies the validity of a cast. If at the time dynamic_cast is executed, the
cast is invalid, then the cast fails. The gener
In all cases, when typeid is applied to a pointer of a non-polymorphic class hierarchy, then the base type of
the pointer is obtained. That is, no determination of what that pointer is actually pointing to is made. As
an experiment, comment-out the virtua
In the program, notice how the static function numObjects( ) is called. In the first two calls, it is called
through its class name using this syntax:
In the third call, it is invoked using the normal, dot operator syntax on an object.
Unnamed namespaces allow you to establish unique identifiers that are known only within the scope of
a single file. That is, within the file that contains the unnamed namespace, the members of that
namespace may be used dire
Because the pointer p is released using delete [ ], the destructor for each object in the array is executed,
as the output shows. Also, notice that because p is indexed as an array, the dot operator is used to
access members of Rectangle.
time and cannot be altered during the execution of a program. The second way information can be
stored is through the use of C+'s dynamic allocation system. In this method, storage for data is
allocated as needed from the free memory area that lies betwee
Inside generic MyClass
Inside MyClass<int> specialization
In the program, pay close attention to this line:
template <> class MyClass<int> cfw_
It tells the compiler that an explicit integer specialization of MyClass is being created.
As the comments inside the program indicate, when swapargs(i, j) is called, it invokes the explicitly
overloaded version of swapargs( ) defined in the program. Thus, the compiler does not generate this
version of the generic swapargs( ) function, because
Using templates, it is possible to create generic functions and classes. In a generic function or class, the
type of data upon which the function or class operates is specified as a parameter. Thus, you can use
one function or class with several different
In this program, the function Xhandler( ) can only throw integer, character, and double exceptions. If it
attempts to throw any other type of exception, then an abnormal program termination will occur. To
see an example of this, remove int from the list a
CRITICAL SKILL 12.1: Exception Handling
An exception is an error that occurs at runtime. Using C+'s exception handling subsystem, you can, in a
structured and controlled manner, handle runtime errors. When exception handling is employed, your
The C+ I/O system maintains status information about the outcome of each I/O operation. The current
status of an I/O stream is described in an object of type iostate, which is an enumeration defined by ios
that includes these members.
To try CompFiles, first copy CompFiles.cpp to a file called temp.txt. Then, try this command line:
CompFiles CompFiles.temp txt
The program will report that the files are the same. Next, compare CompFiles.cpp to a different file,
such as one of the oth
You can detect when the end of the file is reached by using the member function eof( ), which has this
bool eof( );
It returns true when the end of the file has been reached; otherwise it returns false.
peek( ) and putback( )
You can obtain the
Here, the delimiter to get( ) is allowed to default to a newline. This makes get( ) act much like the
standard gets( ) function.
Another function that performs input is getline( ). It is a member of each input stream class. Its
prototypes are s
To read or write binary data, you open a file using what mode specifier?
What does get( ) do? What does put( ) do?
What function reads a block of data?
CRITICAL SKILL 11.10: More I/O Functions
The C+ I/O system defines other I/O related functions
What class creates an input file?
What function opens a file?
Can you read and write to a file using < and >?
CRITICAL SKILL 11.9: Unformatted and Binary I/O
While reading and writing formatted text files is very easy, it is not always the most e
To open an output stream, it must be declared as class ofstream. A stream that will be performing both
input and output operations must be declared as class fstream. For example, this fragment creates one
input stream, one output stream, and one stream ca
The ios:trunc value causes the contents of a preexisting file by the same name to be destroyed, and the
file to be truncated to zero length. When creating an output stream using ofstream, any preexisting file by
that name is automatically truncated.
CRITICAL SKILL 10.5: Using I/O Manipulators
The C+ I/O system includes a second way in which you can alter the format parameters of a stream.
This method uses special functions, called manipulators, that can be included in an I/O expression. The
In addition to the formatting flags, there are three member functions defined by ios that set these
additional format values: the field width, the precision, and the fill character. The functions that set
these values are width( ), precision( ), and fill(
These values are used to set or clear the format flags. Some older compilers may not define the fmtflags
enumeration type. In this case, the format flags will be encoded into a long integer.
When the skipws flag is set, leading whitespace characters (spac
As explained, the I/O library creates two specific versions of the I/O class hierarchies: one for 8-bit
characters and one for wide characters. This book discusses only the 8-bit character classes since they
are by far the most frequently used. Here is a
To summarize: In C+, I/O is performed through a logical interface called a stream. All streams have
similar properties, and every stream is operated upon by the same I/O functions, no matter what type of
file it is associated with. A file is the actual ph
<iostream>. For the most part, the two libraries appear the same to the programmer. This is because the
new I/O library is, in essence, simply an updated and improved version of the old one. In fact, the vast
majority of differences between the two occur