You are on page 1of 21

Programming

Fundamentals

1
A CASE STUDY
 General Crates, Inc. (GCI) builds custom-designed
wooden crates.
 GCI can build wooden crates of any size
 You have been asked to write a program that calculates
the:
 Volume (in cubic feet)
 Cost
 Customer price
 Profit of any crate GCI builds
Continued…
Continued…
THE PROGRAM
TYPE CONVERSION

6
WHEN YOU MIX APPLES WITH
ORANGES: TYPE CONVERSION
 Operations are performed between operands of the same
type.
 If not of the same type, C++ will convert one to be the type of
the other
 This can impact the results of calculations.
 C++ follows data type conversion rules when performing
mathematical operations on variables of different data types.
HIERARCHY OF TYPES
Highest:
long double
double
float
unsigned long
long
Lowest:
unsigned int
int
Ranked by largest number they can hold
TYPE COERCION
 C++ strives to convert the operands of mathematical
operator to the same type if these are different.
 Type Coercion: automatic conversion of an operand to
another data type
 Promotion: convert to a higher type
 Demotion: convert to a lower type
COERCION RULES
Rule#1

When char, short, unsigned short are used in a


mathematical operation, these are automatically
promoted to int
COERCION RULES
Rule#2
When operating on values of different data types, the lower
ranked one is promoted to the type of the higher one.

int yeares;
float interestRate;
..
Cout<<years * interestRate;

//Before the multiplication takes place, years will


be promoted to a float .
COERCION RULES
Rule#3
When using the = operator, the type of expression on right
will be converted to type of variable on left

int x, y = 4;
long int area; float z = 2.7;
int length, width; x = y * z;
..
area = length * width; //In the expression y * z , y will be
promoted to float and 10.8 will result
//The result of the multiplication will be from the multiplication.
converted to long so it can be stored in
area . Since x is an integer, however, 10.8 will
be truncated and 10 will be stored in x .
OVERFLOW AND
UNDERFLOW

13
OVERFLOW AND UNDERFLOW
 Occurs when assigning a value that is too large
(overflow) or too small (underflow) to be held in a
variable
 a , b , and c are all short integers:

a = b * c;
 If b and c are set to values large enough, the
multiplication will produce a number too big to be stored
in a called the overflow situation.
When an Integer overflow
• Typically, when an integer overflows, its contents
wrap around to that data type’s lowest possible value
& vice versa

• No warning/errors given because all happens during


runtime

• The program will use the incorrect number and


therefore produce incorrect results.
OVERFLOW AND UNDERFLOW
 When floating-point variables overflow or underflow, the
results depend upon the system.
 Here reaction will vary and may
 Produces an incorrect result and continues running.
 stop the program,
 display a warning/error message,
TYPE CASTING

17
TYPE CASTING
 A type cast expression lets you manually promote or
demote a value i.e. do manual conversion
 The general format of a type cast expression is

static_cast< DataType >( Value )


 Type cast expressions are useful in situations where C++ will
not perform the desired conversion automatically.
TYPE CASTING
 Useful for floating point division using ints: double
m;
m = static_cast<double>(y2-y1)
/(x2-x1);
 Useful to see int value of a char variable:
char ch = 'C';
cout << ch << " is "
<< static_cast<int>(ch);
TYPE CASTING IN PROGRAM
C-STYLE AND PRESTANDARD TYPE
CAST EXPRESSIONS
 C-Style cast: data type name in ()
cout << ch << " is " <<
(int)ch;
 Prestandard C++ cast: value in ()
cout << ch << " is " <<
int(ch);
 Both are still supported in C++, although
static_cast is preferred

You might also like