c++ primitive types c++ basic types and i/o...

16

Click here to load reader

Upload: dinhcong

Post on 25-Apr-2018

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

1C++ Primitive Types

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 Probable characteristics

float 32-bit IEEE single-precision type

double 64-bit IEEE double-precision type

#include <stdint.h>

int8_t uint8_t

int16_t uint16_t

int32_t uint32_t

int64_t uint64_t

Page 2: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

2C++ Primitive Types

Character types Probable characteristics

char 1-byte, ASCII code

unsigned char 1-byte, unsigned integer

Logical types Probable characteristics

bool 1-byte, value either true or false

See the CS 1044 notes on C++ Fundamentals for more details and examples.

The primitive types are all available without any inclusions from the Standard Library.

Page 3: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

3C++ Variable Declarations

In C++, the following declarations have some fundamentally different effects than their counterparts in Java:

char middleInitial;

int numOfMsgs;

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.

Page 4: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

4C++ Arithmetic Operators

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

Page 5: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

5C++ Input/Output Library

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 dataSource; // input file stream objectofstream Report; // output file stream object

header file: <fstream>

Page 6: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

6Stream Object Internals

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.

Page 7: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

7C++ Input: operator>>()

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.

Page 8: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

8C++ Input Manipulator

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.

Page 9: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

9C++ Output: operator<<()

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:

420Q

See the CS 1044 notes on C++ I/O for more details and examples, including formatting.

Page 10: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

10C++ Output Manipulators

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.

Page 11: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

11Conditionals and Loops

C++ includes the same set of conditional and loop statement forms as Java:

if…

if…else…

switch…

while…

for…

do…while…

C++ also inherits a goto statement for unconditional branching from C.

Thou shalt not goto.

Page 12: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

12Primitive Typecasts

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.

Page 13: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

13C++ typedef Statement

C++ includes a facility for declaring an alias for an existing type:

typedef unsigned int uint32;

typedef char PID[9];

This does not create new types, only convenient names for specialized uses of existing types.

Page 14: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

14C++ enum Types

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 timeOfYear;

. . .

if ( timeOfYear == WINTER )

cout << "Brr..." << endl;

This supports the creation of useful, limited types whose values are meaningful labels.

Page 15: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

15C++ LiteralsC++ 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;

Page 16: C++ Primitive Types C++ Basic Types and I/O 1courses.cs.vt.edu/~cs2605/spring08/.../PDF/L02.C++BasicTypesandIO.pdfData Structures & OO Development I C++ Primitive Types 1 Standard

Computer Science Dept Va Tech January 2008 ©2006-08 McQuain & Barnette

C++ Basic Types and I/O

Data Structures & OO Development I

16C++ string TypeThe 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.