L11 - Implicit Type Conversions-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Recap

Type Conversions

Types, Variables and Input


L11 - Implicit type conversions

Department of Computer Science


University of Pretoria

14 March 2024

COS132 Types, Variables and Input 1 / 24


Recap
Recap of Lectures 04 - 06
Type Conversions

In C++, cout is part of the Standard


Library’s iostream header.
It is used to perform output operations
in C++, typically to the standard
output stream (usually the console or
terminal window).
The term ”cout” stands for ”character
output” and is pronounced ”see-out.”

COS132 Types, Variables and Input 2 / 24


Recap
Recap of Lectures 04 - 06
Type Conversions

Cout implementation in C++ code


1 #i n c l u d e <i o s t r e a m >
2 i n t main ( ) {
3 // O u t p u t t i n g a s t r i n g
4 s t d : : c o u t << ” H e l l o , World ! ” << s t d : : e n d l ;
5 // O u t p u t t i n g a number
6 i n t num = 8 ;
7 s t d : : c o u t << ”The v a l u e o f num i s : ” << num << s t d : : e n d l ;
8 return 0;
9 }

COS132 Types, Variables and Input 3 / 24


Recap
Recap of Lectures 04 - 06
Type Conversions

Comments serve as documentation of


C++ code, explaining its purpose,
functionality, and any important details
that might not be immediately obvious
from the code itself.
Well-written comments improve the
readability of your code, making it easier
for other developers (or even yourself, in
the future) to understand what the code
does and how it works.
COS132 Types, Variables and Input 4 / 24
Recap
Recap of Lectures 04 - 06
Type Conversions

In C++ programming, we have two types of


comments. These are:
Single Line Comments: denoted by
// . These are used when you want to
comment on a single line. The // tells
the compiler to ignore everything after
the symbol to the end of the line.

COS132 Types, Variables and Input 5 / 24


Recap
Recap of Lectures 04 - 06
Type Conversions

In C++ programming, we have two types of


comments. These are:
Multi - Line Comments: denoted by
/* and */ pair. This type is used when
you would like to comment on more
than one line. Everything between the
two symbols is ignored by the compiler.

COS132 Types, Variables and Input 6 / 24


Recap
Recap of Lectures 04 - 06
Type Conversions

Layout Rules
Good and consistent code layout
enhances code readability.
If code is readable, it is inherently more
understandable and consequently
reliable and maintainable.

COS132 Types, Variables and Input 7 / 24


Recap
Recap of Lectures 04 - 06
Type Conversions

Pointers to create good layouts


Use indentation and blank lines to reveal
the subordinate nature of blocks of code.
Each line which is part of the body of a
control structure (if, while, dowhile, for,
switch) is indented one tab stop from
the margin of its controlling line.
The same rule applies to function,
struct, or union definitions, and
aggregate initialisers.
COS132 Types, Variables and Input 8 / 24
Recap
Recap of Lectures 04 - 06
Type Conversions

Syntax Errors
Syntax errors occur when the code does
not conform to the syntax rules of the
programming language.
In C++, syntax errors prevent the
compiler from understanding your code
and result in compilation failures.
Syntax errors are usually straightforward
to identify because they violate the
language’s grammar rules.
COS132 Types, Variables and Input 9 / 24
Recap
Recap of Lectures 04 - 06
Type Conversions

Examples of Syntax Errors


Missing semicolons
Mismatched parentheses, braces, or
brackets
Misspelled keywords or identifiers

COS132 Types, Variables and Input 10 / 24


Recap
Recap of Lectures 04 - 06
Type Conversions

Code Example
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3
4 i n t main ( )
5 {
6 i n t cout = 5;
7 i n t team = 4 ;
8 c o u t << \n ”The sum i s ”<< c o u t + team ;
9 return 0;
10 }

Study the above code. Identify any syntax


errors (if any), and give reasons.

COS132 Types, Variables and Input 11 / 24


Recap
Recap of Lectures 04 - 06
Type Conversions

Semantic Errors
Semantic errors occur when the code
executes without crashing, but it does
not produce the expected result due to
incorrect logic or flawed understanding
of the problem.
Unlike Syntax errors, Semantic errors do
not violate the syntax rules, so they do
not result in compilation failures.
COS132 Types, Variables and Input 12 / 24
Recap
Recap of Lectures 04 - 06
Type Conversions

Examples of Semantic Errors


Incorrect Calculations
Type Mismatches
Division by zero

COS132 Types, Variables and Input 13 / 24


Recap
Recap of Lectures 04 - 06
Type Conversions

Code Example
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3
4 i n t main ( ) {
5 i n t num1 = 5 ;
6 c h a r num2 = ’ 7 ’ ;
7
8 int r e s u l t = num1 + num2 ;
9
10 c o u t << ”The sum i s : ” << r e s u l t ;
11 return 0;
12 }

Study the above code. Identify any semantic


errors (if any), and give reasons.

COS132 Types, Variables and Input 14 / 24


Implicit Type Conversions
Recap
Explicit Type Conversions
Type Conversions
More on Type Conversions

Data types used in C++


Type conversions, refer to the process of
converting one data type into another in
C++
Type conversions can occur
automatically (known as implicit
conversions) or can be performed
manually by the programmer (known as
explicit conversions or typecasts)
COS132 Types, Variables and Input 15 / 24
Implicit Type Conversions
Recap
Explicit Type Conversions
Type Conversions
More on Type Conversions

Implicit Type Conversions


An Implicit type conversion occurs
automatically when the compiler detects
that a value of one type needs to be
converted into another type in a
compatible context.
This often happens when assigning a
value of one data type to a variable of
another data type, or when passing
arguments to functions.
COS132 Types, Variables and Input 16 / 24
Implicit Type Conversions
Recap
Explicit Type Conversions
Type Conversions
More on Type Conversions

Implicit Type Conversion Code


Example
1 #i n c l u d e <i o s t r e a m >
2 using namespace s t d ;
3
4 i n t main ( ) {
5 i n t num1 = 5 ;
6 // I m p l i c i t c o n v e r s i o n from i n t t o d o u b l e
7 double num2 = num1 ;
8 return 0 ;
9 }

COS132 Types, Variables and Input 17 / 24


Implicit Type Conversions
Recap
Explicit Type Conversions
Type Conversions
More on Type Conversions

Explicit Type Conversions


Explicit type conversion involves
manually specifying the conversion from
one data type to another using casting
operators.
This allows the programmer to override
the default type conversion behavior and
enforce the desired conversion explicitly.

COS132 Types, Variables and Input 18 / 24


Implicit Type Conversions
Recap
Explicit Type Conversions
Type Conversions
More on Type Conversions

Explicit Type Conversion Code


Example
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3
4 i n t main ( ) {
5 d o u b l e num1 = 5 . 5 ;
6 // E x p l i c i t c o n v e r s i o n from d o u b l e t o i n t
7 i n t num2 = s t a t i c c a s t <i n t >(num1 ) ;
8
9 return 0;
10 }

Here, the static cast operator is used to


explicitly convert the double value 5.5 to
an integer and assign it to the variable
num2.
COS132 Types, Variables and Input 19 / 24
Implicit Type Conversions
Recap
Explicit Type Conversions
Type Conversions
More on Type Conversions

Narrowing or Widening Conversions


Type conversions can involve
narrowing or widening conversions.
A narrowing conversion occurs when
converting a data type with a larger
range to a data type with a smaller
range, potentially resulting in loss of
data.

COS132 Types, Variables and Input 20 / 24


Implicit Type Conversions
Recap
Explicit Type Conversions
Type Conversions
More on Type Conversions

Narrow Type Conversion Code


Example
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3
4 i n t main ( ) {
5 d o u b l e num1 = 5 . 8 ;
6 // N a r r o w i n g c o n v e r s i o n ( i m p l i c i t )
7 i n t num2 = num1 ;
8 return 0;
9 }

In this example, converting a double to


an int may result in truncation of the
decimal part.
COS132 Types, Variables and Input 21 / 24
Implicit Type Conversions
Recap
Explicit Type Conversions
Type Conversions
More on Type Conversions

Widening Conversions
A widening conversion occurs when
converting a data type with a smaller
range to a data type with a larger range,
typically without loss of data.

COS132 Types, Variables and Input 22 / 24


Implicit Type Conversions
Recap
Explicit Type Conversions
Type Conversions
More on Type Conversions

Widening Type Conversion Code


Example
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3
4 i n t main ( ) {
5 i n t num1 = 1 5 ;
6 // W i d e n i n g c o n v e r s i o n ( e x p l i c i t )
7 d o u b l e num2 = s t a t i c c a s t <d o u b l e >(num1 ) ;
8 return 0;
9 }

COS132 Types, Variables and Input 23 / 24


Implicit Type Conversions
Recap
Explicit Type Conversions
Type Conversions
More on Type Conversions

Things to note
Narrowing conversions may lead to
unintended behaviour or loss of
precision.
Using explicit type conversions through
casting operators can help make the
conversion process more transparent and
explicit in the code, reducing the
likelihood of errors.
COS132 Types, Variables and Input 24 / 24

You might also like