Chapter 3

advertisement
Chapter 3 – Pg. 1
Chapter 3
NUMERIC TYPES, EXPRESSIONS, AND OUTPUT
CHAPTER OUTLINE
I. Overview of C++ Data Types - the C++ built-in data types are organized into simple, structured,
and address types.
II. Numeric Data Types
A. Integral Types - represent integers - int - 2 bytes or long - 4 bytes.
a. Char, short, int, and long - no fraction or commas allowed.
b. Unsigned - only positive or zero - signed - 2 bytes.
c. Represent different sizes of integers, from smaller (fewer bits) to larger (more bits).
The size is machine dependent.
d. Int - from -32768 to +32768 or -2147483648 to +2147483648 - usually long.
e. If overflow - use long.
f. Do not start a literal constant with a 0 - thinks it is an octal format.
B. Floating-Point Types - represent real numbers with decimal points - 4 bytes or double 8
bytes.
a. Represent real numbers - integer part and a fractional part - exact size is machine
dependent.
b. Starting with a 0 is o.k., not considered octal format.
c. Float type is usually sufficient precision.
III. Declarations for Numeric Types - declare constants - no quotes if numeric; quotes if string or
char.
A. Name Constant Declarations - must start with a letter so as not confused with numbers neither have quotes, use all caps to distinguish from variables. Constants are either numeric
or alphanumeric. Numeric constants are used in calculations, all others are alphanumeric.
Numeric constants can be either integers or floating point values.
Software Engineering Tip: Using Named Constants Instead of Literals - always a good idea,
makes easier to modify and more readable. The process is much simpler and more reliable.
B. Variable Declarations
IV. Simple Arithmetic Expressions
A. Arithmetic Operations - expressions are made up of constants, variables, and operators. The
operators allowed depend on the data types of the constants and variable included.
a. Unary operators - one operand required - one factor.
b. Binary operators - two operands required - two factors.
c. % - modulus - only used with integers and returns an integer quantity indicating the
remainder of a division expression. It performs a division and then returns the
remainder as the result of the operation. It is handy when doing conversions that
are not base 10. For example when converting from minutes to hours - it is a
fraction of 60 not 100.
d. The computer cannot divide by zero - will result in a run-time error.
e. The same variable can appear on both sides of the assignment operator - i.e.
Subtotals.
B. Increment and Decrement Operators - take a single name variable name as an operand - add
one or subtract 1, i.e. num++ is the same as num=num+1. ++ or -- can be either prefix
operators, ++num or postfix operators num++. ++ or -- can be in the middle of an expression.
They should only be used in stand-alone statements.
V. Compound Arithmetic Expressions
A. Precedence Rules.
Chapter 3 – Pg. 2
1. Binary arithmetic operators may cause a conversion of the data type depending on the
operator and on the type of the operands. This is known as arithmetic conversion. If
two fields of different types are used, the compiler will 'promote' the type of the
operand from the less accurate type to the more accurate type. The programmer
should try not to mix data types, but sometimes it is necessary, or is done by mistake.
B. Type Coercion and Type Casting - happens when integer and floating-point values are
combined in an assignment statement or an arithmetic expression.
C. When working with floating point output - default
1. Large and small numbers show in E notation and are system-dependent.
2. The number of decimal places is system dependent.
3. No decimal point is displayed for whole numbers.
4. Assignment Statement
a. Implicit conversion - automatic conversion of a value from one data type to another type coercion.
b. Explicit conversion - conversion is controlled by the programmer - sometimes will
make the program clearer and more error free.
c. The only difference between the two is clarity. With cast operations, it is clear the
programmer is mixing data types intentionally.
5. Arithmetic Expressions - mix data types within an expression - implicit type coercion
occurs. It is best to avoid mixing values of different types within an expression.
May We Introduce: Blaise Pascal
VI. Function Calls and Library Functions - when the function is to be executed, it is called by
referencing the function name.
1. A semicolon follows the function name because the call is a statement.
2. The function may be called as many times as necessary.
3. The computer temporarily puts the calling function on hold and starts the called function.
When the called function is finished, the computer goes back to the calling function and
resumes where it left off.
4. The function invocation, or argument or parameter, is the provided value to be used by
the called function. This allows the called function to work on many different values.
The argument list is the way functions communicate with each other. Function calls
have higher precedence than multiplication/division.
A. Value-Returning Functions - returns the result of the function (only one result) - called
within an expression - does not appear as a separate statement.
B. Void functions - perform a procedure - does not return a value - called as a separate, standalone statement. It performs some action and then quits. It begins with the word void.
C. Library Functions - a large collection of prewritten functions, data types, and other items.
Using a library function is easy - place an #include directive at the top of the program
specifying which one is included. Forgetting to include header files can cause a lot of
wasted time looking for compiler errors that are do to not including header files.
VII. Formatting the Output - control how it appears on the screen or from the printer.
a. You can print variables using the output objects. Insertion operators separate each item on
an output line.
b. The output of the data can be controlled with features called manipulators. They are
contained in the iomanip file, which must be included for their use.
c. Integers and Strings - be sure to print blanks between numbers.
d. Manipulators - causes some action to occur; are only used in input and output statements.
e. Many manipulators included in the C++ standard library
f. setw - set width - lets the programmer control how many character positions the next data
item should occupy when it is output.
1. It contains the number of characters in the column.
2. It is only for formatting numbers and strings, not char data.
3. The argument is an integer expression called the field width specification - the group of
character positions is called the field.
4. The next data item is printed right-justified.
Chapter 3 – Pg. 3
5.
It is better to set the field width larger than the minimum size to allow for some space
between fields.
6. It is a one-time action. It holds only for the very next item to be output - only applies to
the field following it.
B. Floating-Point Numbers - decimal point counts as a position.
1. Fixed is used to force all subsequent floating-point output to appear in decimal form
rather than scientific notation.
2. Showpoint is used to force decimal points to be displayed.
3. Setprecision is used to control the number of decimal places. It sets the rounding
accuracy for a decimal number. It remains in effect until another setprecision is
encountered.
C. Program formatting - C++ statements are free format. They can appear anywhere on a line,
more than one can appear on a single line, and one statement can span several lines. The
compiler only needs blanks to separate important symbols and semi-colons to terminate
statements. You should use indentation to make the program more readable.
VIII. Additional String Operations
A. The length and size Functions
1. The length function returns an unsigned integer value indicating the number of characters
in a string. The format is a dot notation - the dot is between the variable name and the
function name, i.e. fullname.length(). The length of a string is its size. The size
function will return the same value.
2. size_type is part of the identifiers contained in the standard library. It is for use when you
are not sure whether to go with int or long.
B. The find Function - searches a string for the first occurrence of a substring. It returns the
position where the match occurs, starting with 0. The match must be exact and is case
sensitive. It can be words or parts of words and only returns the first match.
C. The substr Function -returns a substring from a string.
1. Contains two arguments - first - position to start, second - how many characters to grab.
Software Engineering Tip: Understanding Before Changing - If an error in your program is
encountered, it is better to carefully and thoroughly think through the solution than to quickly
start changing things.
Download