C++ Primer Plus (6th ed.) CH 3: Dealing with Data

I Simple variables

1) Names for variables

– Allowed characters are alphabetic characters, numeric digits and the underscore character

– 1st character cannot be a numeric digit

– Names beginning with two underscore characters or with an underscore character followed by an uppercase letter are reserved for use by the implementation. Names beginning with a single underscore character are reserved for use as global identifiers by the implementation.

– C++ places no limits on the length of a name. But some platforms might have their own length limits.

2) Integer types

– short at least 16 bits wide

– int at least as big as short

– long at least 32 bits wide and at least as big as int

– long long at least 64 bits wide and at least as big as long

– sizeof operator returns the size, in bytes, of a type or a variable. For types parentheses are necessary. But not for variables.

– climits header file contains information about integer type limits

CHAR_BIT: no. of bits in a char

CHAR_MAX: max. char value

CHAR_MIN: min. char value

SCHAR_MAX: max. signed char value

SCHAR_MIN: min. signed char value

UCHAR_MAX: max. unsigned char value

SHRT_MAX: max. short value

SHRT_MIN: min. short value

USHRT_MAX: max. unsigned short value

INT_MAX: max. int value

INT_MIN: min. int value

UINT_MAX: max. unsigned int value

LONG_MAX: max. long value

LONG_MIN: min. long value

ULONG_MAX: max. unsigned long value

LLONG_MAX: max. long long value

LLONG_MIN: min. long long value

ULLONG_MAX: max. unsigned long long value

– a better way to create symbolic constants other than define is const

– Unique initialization syntax of C++: int wrens(432);

– Initialization with C++11:

int rheas = {12};

int emus{7}; // = not necessary

int rocs{}; // Initialized to be 0

which provides better protection against type conversion errors

– unsigned by itself is short for unsigned int

3) Choosing an integer type

– int is set to the most natural integer size for the target computer, with natural size referred to the integer form that the computer handles most efficiently

– iostream header file provides the dec, hex, and oct manipulators to give cout the messages to display integers in decimal, hexadecimal, and octal formats, respectively

4) How C++ decides what type a constant is

– Use suffixes placed at the end of a numeric constant to indicate the type, e.g.; 22022L

5) The char type: characters and small integers

– cout.put(): a member function (which belongs to a class and describes a method for manipulating class data) to display a char. The period is called the membership operator. The notation means to use the class member function put() with the class object cout

– escape sequences:

\n – newline

\t – horizontal tab

\v – vertical tab

\b – backspace

\r – carriage return

\a – alert

\\ – backslash

\? – question mark

\’ – single quote

\” – double quote

– Universal character names begin with either \u or \U, followed by 8 and 16 hexadecimal digits, respectively

– char is not signed or unsigned by default. The choice is left to the C++ implementation in order to allow the computer developer to best fit the type to the hardware properties

– Wide character type: wchar_t. A wide-character constant or string is indicated by preceding it with an L, e.g.,

wchar_t bob = L’P’;

– New C++11 types: char16_t and char32_t, with u prefix for the former and U for the latter

– The bool type can be assigned to true or false. Any nonzero value converts to true, whereas a zero value converts to false

II The const qualifier

– The keyword const is termed a qualifier because it qualifies the meaning of a declaration

– const <type> <name> = value;

– Better than the #define statement: lets you specify the type explicitly; possible to use C++’s scoping rules to limit the definition to particular functions or files; possible to use const with more elaborate types, such as arrays and structures

III Floating-point numbers

1) Floating-point types

– float, double and long double

– float at least 32 bits for significant digits, double at least 48 digits, and long double at least as big as double

– the minimum number of significant digits: DBL_DIG, FLT_DIG and LDBL_DIG for double, float and long double, respectively

– setf() forces output to stay in fixed-point notation and prevents the program from switching to E notation for large values:

cout.setf(ios_base::fixed, ios_base::floatfield);

2) Floating-point constants

– double by default

– f or F suffix for float, l or L for long double

3) Pros and cons of floating-point numbers

– floating point operations usually are slightly slower than integer operations and precision can be lost

IV C++ arithmetic operators

1) Order of operation: operator precedence and associativity

– When two operators have the same precedence, C++ looks at whether the operators have a left-to-right or right-to-left associativity

2) Division diversions

– Operator overloading: the process of using the same symbol for more than one operation

int / int – int division

long / long – long division

double / double – double division

float / float – float division

3) Type conversions

– C++ converts values when assigning a value of one type to a variable of another type, such as from double to float, float to int, and long to short

– An initialization using braces is called a list-initialization, which doesn’t permit narrowing (when the type of the variable may not be able to represent the assigned value)

– When an operation involves two types, the smaller is converted to the larger

– C++ promotes float arguments to double when passing them to a function that waives prototyping

– Type cast comes in two forms:

(<typeName>) <value>; // straight C

or

<typeName> (<value>); // pure C++

– C++ introduces four type cast operators, e.g.,

static_cast<typeName> (<value>);

4) auto declarations in C++11

– auto <variable> = <value>;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s