C Primitive Types C Basic Types and IO

C++ Primitive Types C++ Basic Types and I/O 1 Standard C++ provides a plethora of primitive types. These store single values, and are most definitely not objects in the Java sense. In particular, there is no guarantee of automatic initialization. Integer types Probable characteristics int 32 -bits unsigned int 32 -bits short (int) 16 -bits unsigned short (int) 16 -bits long (int) 32 -bits unsigned long (int) 32 -bits Floating-point types #include <stdint. h> int 8_t uint 8_t int 16_t uint 16_t int 32_t uint 32_t int 64_t uint 64_t Probable characteristics float 32 -bit IEEE single-precision type double 64 -bit IEEE double-precision type Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ Primitive Types Character types C++ Basic Types and I/O 2 Probable characteristics char 1 -byte, ASCII code unsigned char 1 -byte, unsigned integer Logical types bool Probable characteristics 1 -byte, value either true or false The primitive types are all available without any inclusions from the Standard Library. See the CS 1044 notes on C++ Fundamentals for more details and examples. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ Variable Declarations C++ Basic Types and I/O 3 In C++, the following declarations have some fundamentally different effects than their counterparts in Java: char middle. Initial; int num. Of. Msgs; First of all, there is no automatic initialization for variables of primitive types. This opens up all sorts of nasty consequences. Second, the variables declared here are NOT references; they are simply names of locations in memory. This is true in general in C++. We will explore dynamic allocation of variables shortly. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ Arithmetic Operators C++ Basic Types and I/O 4 Same syntax as Java. Semantics are generally the same as well, although the C++ Standard leaves the result of a number of unwise constructs undefined. For example: x = x++; Precedence rules are the same as Java. Precedence can be forced by use of parentheses. Equality versus Identity - identity means two objects are, in fact, the same object - equality means two objects have the same value (but may or may not be identical) - in C++, operator==() means equality, not identity Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ Input/Output Library C++ Basic Types and I/O 5 The basic data type for I/O in C++ is the stream. C++ incorporates a complex hierarchy of stream classes. The most basic stream classes are the standard input/output streams: istream cin built-in input stream variable; by default hooked to keyboard ostream cout built-in output stream variable; by default hooked to console header file: <iostream> C++ also provides stream types for reading from and writing to files: ifstream data. Source; ofstream Report; // input file stream object // output file stream object Computer Science Dept Va Tech January 2008 header file: <fstream> Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

Stream Object Internals C++ Basic Types and I/O 6 Each stream object has an internal buffer (streambuf) that stores a selected sequence of bytes from the device. Normally you won't care about this. Input streams maintain an internal pointer, called the get pointer, to the next byte that will be read. Output streams maintain an internal pointer, called the put pointer, to the location to which the next byte will be written. Streams maintain a number of internal status flags. The most important is the fail bit, which is tested by the fail() member function. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ Input: operator>>() C++ Basic Types and I/O 7 The simplest, and most common, way to read input is to use the extraction operator (>>): int X, Y; char Z; cin >> X; cin >> Y >> Z; Hint: the extraction operator (>>) points in the direction the data is flowing. By default the extraction operator discards leading whitespace characters, and then parses the input stream to construct a value corresponding to the type of the target variable. It is possible for an input operation to fail, if the stream is empty or if its contents are incompatible with the target variable. In that case, the effect on the target variable is unspecified, and the input stream will set a state flag indicating it is in a fail state. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ Input Manipulator C++ Basic Types and I/O 8 The Standard Library provides the following input manipulator: ws extracting to this advances the stream pointer to the next non-whitespace character Manipulators are generally made available through the Standard header file iomanip. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ Output: operator<<() C++ Basic Types and I/O 9 The simplest, and most common, way to write output is to use the insertion operator (<<): int X = 42, Y = 0; char Z = 'Q'; cout << X; cout << Y << Z; Hint: the insertion operator (<<) points in the direction the data is flowing. The insertion operator converts the value of the source variable into ASCII text and places the resulting characters into the output stream. Formatting is entirely up to you. The code fragment above would write the following output to the console: 420 Q See the CS 1044 notes on C++ I/O for more details and examples, including formatting. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ Output Manipulators C++ Basic Types and I/O 10 The most common way to format output in C++ is to use manipulator objects supplied by the Standard Library: endl inserts end-of-line marker ('n') setw(n) causes the following value to be placed into a field of n columns left causes the following value to be left-justified in its field right causes the following value to be right-justified in its field setfill('x') sets the fill character to be 'x' (default is a space) hex causes the following (numeric) value to be written in base-16 setprecision(n) causes n digits to be shown to the right of the decimal point There are other manipulators in the Standard Library, and it is possible to implement your own, custom manipulators. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

Conditionals and Loops C++ Basic Types and I/O 11 C++ includes the same set of conditional and loop statement forms as Java: if…else… switch… while… for… do…while… C++ also inherits a goto statement for unconditional branching from C. Thou shalt not goto. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

Primitive Typecasts C++ Basic Types and I/O 12 C++ allows sensible explicit casts amongst the primitive types: double x = 6. 4, y; int z = 42, w; y = (double) z; // C/Java style cast y = double(z); // C++ style cast y = z; // implicit cast, same result as above w = (int) x; // integer value is truncated y = static_cast<double>(z); // new-style cast The implicit casts are problematic; compiler may or may not issue a warning. Good style always makes typecasts explicit. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ typedef Statement C++ Basic Types and I/O 13 C++ includes a facility for declaring an alias for an existing type: typedef unsigned int uint 32; typedef char PID[9]; This does not create new types, only convenient names for specialized uses of existing types. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ enum Types C++ Basic Types and I/O 14 C++ includes a facility for declaring a new data type by listing (enumerating) the set of values for variables of that type: enum Season {SPRING, SUMMER, FALL, WINTER}; Season time. Of. Year; . . . if ( time. Of. Year == WINTER ) cout << "Brr. . . " << endl; This supports the creation of useful, limited types whose values are meaningful labels. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ Literals C++ Basic Types and I/O 15 C++ allows numeric and character-based literals: cout << "Hello, world" << endl << "pi is about " << 3. 14159 << endl << "and the first letter in the alphabet is " << 'A' << endl; Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette

C++ string Type C++ Basic Types and I/O 16 The C++ Standard Library includes a class string for storing and manipulating character sequences of essentially arbitrary length. string Soliloquy; Soliloquy = "To be, or not to be, that is the question. "; cout << "So, then this guy says: "" << Soliloquy << '"' << endl; See the CS 1044 notes on C++ I/O and on String Operations for more details and examples, including advanced input and string member functions. Computer Science Dept Va Tech January 2008 Data Structures & OO Development I © 2006 -08 Mc. Quain & Barnette
- Slides: 16