0% found this document useful (0 votes)
57 views

Important Review: Behrang Parhizkar (Hani)

This document provides an overview and objectives of a lecture on C++ review. It covers fundamental C++ data types like integers, floating point values, characters, and Booleans. It also discusses programmer-defined data types using typedefs and enums. Additionally, it reviews pointers, dynamic memory allocation, arrays, templates, and the vector STL. The lecture aims to review these core C++ concepts.

Uploaded by

Sello Hlabeli
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Important Review: Behrang Parhizkar (Hani)

This document provides an overview and objectives of a lecture on C++ review. It covers fundamental C++ data types like integers, floating point values, characters, and Booleans. It also discusses programmer-defined data types using typedefs and enums. Additionally, it reviews pointers, dynamic memory allocation, arrays, templates, and the vector STL. The lecture aims to review these core C++ concepts.

Uploaded by

Sello Hlabeli
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Lecture 1

Important Review

Behrang Parhizkar (Hani)

1
Contents
C++'s Simple Data Types
Programmer-Defined Data Types
Pointers
Dynamic Memory Allocation
Arrays
Dynamic Arrays
Pointer Arithmetic
Templates
vector STL

2
Lecture Objectives
Review on C++
Pointer
Dynamic Memory Allocation
Arrays
function templates
class templates
vector STL.

3
C++ Types

Fundamental Types Structured Types


1. Void
2. Pointers
3. Arithmetic

integral Floating point bool complex

1. array
1. characters 1. integers 2. struct
2. char 2. int
3. vector
3. unsigned char 3. Short int
4. signed char 4. Long int 4. set
5. unsigned 5. list
6. unsigned short 6. stack
7. unsigned long 7. queue
8. tree
9. graph
C++ Simple Data Types
Integers
Unsigned integers (cardinal number)
The set of non-negative number. {0,1,2,3,}
unsigned short: values are stored in 2 bytes.
Unsigned:
unsigned long: values are stored in 4 bytes.
Signed integers
The set of negative and positive numbers.
{,-3,-2,-1,0,1,2,3,}
short, int, long

5
Two's Complement Representation
For nonnegative n:
Use ordinary base-two representation with
leading (sign) bit 0
For n < 0
1) Find w-bit base-2 representation of |n|
2) Complement each bit.
3) Add 1

6
Two's Complement Representation
Example: 88
1. 88 as a 16-bit base-two number
0000000001011000
2. Complement this bit string
1111111110100111
WHY?
3. Add 1
1111111110101000

7
Two's Complement Representation
Works well for arithmetic computations

5 + 6:
0000000000000101
+1111111111111010
1111111111111111
What gets done to the
bits to give this
answer?

8
Problems with Integer Representation

Limited Capacity a finite number of bits


An operation can produce a value that requires more
bits than maximum number allowed.

This is called overflow .

None of these is a perfect representation of


(mathematical) integers
Can only store a finite (sub)range of them.

9
C++ Simple Data Types
Real Data (floating point values)
Types float and double in C++
Type double is the default type used for processing real
data.
A suffix F can be attached to a real literal as in 123.45F
to specify that it is to be processed as a float value.
Attaching L specifies a long double value.
Store:
sign of mantissa in leftmost bit (0 = +, 1 = )
represent exponent in next 8 bits
(exponent + 127)
bits b2b3 . . .b24 mantissa in rightmost 23 bits.
Need not store b1 (we know it's 1)

10
Real Data
Example: 22.625 = 10110.1012
Floating point form:
1.01101012 * 24

11
Problems with Real Representation
Exponent overflow and underflow

Round off error


Most reals do not have terminating binary
representations.
Example:
0.7 = (0.10110011001100110011001100. . .)2

12
Problems with Real Representation
Round off error may be compounded in a sequence
of operations.
Real-world example Gulf War Patriot missile guidance
affected by accumulated round off

Be careful in comparing reals


with == and !=.
Instead use comparison for closeness
if (abs (x 12.34) < 0.001)

13
C++ Simple Data Types
Character Data
1 byte for ASCII, EBCDIC
2 bytes for Unicode (java)
or C++ wide character type

Operations ==, <, >, etc. Using numeric code

14
C++ Simple Data Types
Boolean Data
Values { false, true }
Could be stored in bits, usually use a byte
Operations &&, ||
In C++
bool type
int (boolVal) evaluates to
0 if false
1 if true

15
Programmer-Defined Data Types
Typedefs
Mechanism usable to create a new type
Give new name to existing type
Example:
typedef double real;
Now either double or real can be used.

16
Programmer-Defined Data Types
Enumerations
Mechanism for creating types whose literals are
identifiers
Each identifier associated with unique integer

17
Programmer-Defined Data Types
Also possible to specify explicit values to give
the enumerators
enum NumberBase { BINARY = 2,
OCTAL = 8,
DECIMAL = 10,
HEXADECIMAL = 16};

18
Pointers
int *intVar; *intVar = 27;
When regular variables are
declared
Memory allocated for value 27
of specified type
Variable name associated
with that memory location
Memory initialized with
values provided (if any)

19
Pointers
Pointer Variables
value stored is a memory address
Note sample program, Fig. 2.4
Declares variables that can store int addresses
and double variables
Displays the addresses

20
Basic Pointer Operations
Dereferencing and indirection
Pointer variable stores address of a location
Accessing contents of that location requires
dereferencing operator *

Note program example


Fig 2.5

21
Basic Pointer Operations
Assignment
Pointer variables can be assigned the values of
other pointer variables bound to same type

22
Basic Pointer Operations
Consider *jPtr = 44;
Changes value that both pointers reference

Not good programming practice, hard to debug


Known as aliasing problem

23
Basic Pointer Operations
Comparison
Relational operators used to compare two
pointers
Must be bound to same type
Most common = = and !=
The null address may be compared with any
pointer variable

24
Dynamic Memory Allocation
The new operation
Example
int * intPtr;
intPtr = new int;

An anonymous variable
Cannot be accessed directly
25
Pointer Arguments
Pointers can be passed as arguments to
functions

This is logically equivalent to reference


parameters
In fact, this is how early C++ compilers
accomplished reference parameters
26

You might also like