Review 1 C++ Programming 2
Review 1 C++ Programming 2
2
OBJECTIVES
Items that will be reviewed:
Identifiers
Literals vs. constants
Arithmetic precision, type casting, and decimal format
Shorthand notation / Prefix and postfix
The optional else
Conditional operator
Functions and passing parameters
Function overloading
Arrays and vectors: capacity, size and number of elements
The const modifier for parameters
3
IDENTIFIERS, LITERALS AND
CONSTANTS
4
IDENTIFIERS
An identifier is a name given to a variable, a
constant, a function, an object, a class…
int myInteger = 3;
vector<int> myVector;
MyClass myObject;
Do not abbreviate
(see syllabus)
6
LITERAL DATA
Literals
Examples:
2 // Literal constant int
5.75 // Literal constant double
‘Z’ // Literal constant char
"Hello World" // Literal constant string
7
CONSTANTS
Literals are "OK", but provide little meaning
For example, seeing the number 24 throughout your
code, tells nothing about what it represents
10
NUMBERS
11
ARITHMETIC PRECISION
Precision of calculations
VERY important consideration!
Expressions in C++ might not evaluate as you would
"expect"!
"Highest-order operand" determines type of arithmetic
"precision" performed
Common error!
12
ARITHMETIC PRECISION (CONT.)
Examples:
17 / 5 evaluates to 3 in C++
Both operands are integers
Integer division is performed and gives incorrect result
int n1 = 1,
n2 = 2;
cout << (n1/ n2);
(more…) 13
ARITHMETIC PRECISION (CONT.)
Calculations done "one-by-one"
1 / 2 / 3.0 / 4 performs 3 separate divisions
First 1 / 2 equals 0
Then 0 / 3.0 equals 0.0
Then 0.0 / 4 equals 0.0!
14
TYPE CASTING
Casting for variables
Can add ".0" to literals to force precision arithmetic
int num = 2;
double x = static_cast<double>(num) / 2;
cout.setf(ios::fixed);
cout.setf(ios::showpoint); //shows point even if 0
cout.precision(2); //shows 2 decimals
Option 2:
#include <iomanip>
...
cout << fixed << showpoint << setprecision(2);
16
SHORTHAND NOTATION
17
SHORTHAND NOTATIONS
Use them!
18
PREFIX AND POSTFIX
Post-Increment
int n1 = 3;
int n2 = n1++;
Uses current value of variable, THEN increments it
Pre-Increment
int n1 = 3;
int n2 = ++n1;
Increments variable first, THEN uses new value
int firstNumber = 2,
secondNumber = 3;
20
PREFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
21
PREFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
22
PREFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
23
PREFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
24
PREFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
OUTPUT:
25
POSTFIX IN EXPRESSIONS
int firstNumber = 2,
secondNumber = 3;
26
POSTFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
27
POSTFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
28
POSTFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
OUTPUT:
3
29
POSTFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
Condition is
evaluated again.
OUTPUT:
3
30
POSTFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
OUTPUT:
3
31
POSTFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
OUTPUT:
3
32
POSTFIX IN EXPRESSIONS (CONT.)
int firstNumber = 2,
secondNumber = 3;
OUTPUT:
3
4 33
PREFIX AND POSTFIX
As we saw, prefix and postfix might change the
results of the statement.
int firstNumber = 2,
secondNumber = 3; OUTPUT:
int firstNumber = 2,
secondNumber = 3;
Note:
Nothing to do for false condition, so there is no else clause!
Execution continues with cout statement 36
CONDITIONAL OPERATOR
Conditional operator, also called
"ternary operator"
Essentially "shorthand if-else" operator
Can be written:
37
"?" and ":" form the "ternary" operator
CONDITIONAL OPERATOR
Avoid using the conditional operator in an
output expression, because misplacing
parenthesis can produce unwanted results:
38
FUNCTIONS
39
FUNCTIONS
Two types:
void
Does not return a value
return a value
In C++
Only one value can be returned
41
FUNCTION DEFINITIONS
Function definition
Syntax
43
PASSING ARGUMENTS BY VALUE
When passing arguments by value, any changes made to
the function parameter will not change the argument.
int main( )
{
int n = 3;
myFunction(n);
cout << n;
...
}
void myFunction (int n)
{
++n;
cout << n << endl; 44
}
PASSING ARGUMENTS BY VALUE
When passing arguments by value, any changes made to
the function parameter will not change the argument.
int main( )
{
int n
int n = 3;
myFunction(n); 3
cout << n;
...
}
void myFunction (int n)
{
++n;
cout << n << endl; 45
}
PASSING ARGUMENTS BY VALUE (CONT.)
When passing arguments by value, any changes made to
the function parameter will not change the argument.
int main( )
{
int n
int n = 3;
myFunction(n); 3
cout << n;
... call to myFunction (3)
}
void myFunction (int n)
{
++n;
cout << n << endl; 46
}
PASSING ARGUMENTS BY VALUE (CONT.)
When passing arguments by value, any changes made to
the function parameter will not change the argument.
int main( )
{
int n
int n = 3;
myFunction(n); 3
cout << n;
...
}
void myFunction (int n) int n
{ 3
++n;
cout << n << endl; a local copy of n 47
} is created
PASSING ARGUMENTS BY VALUE (CONT.)
When passing arguments by value, any changes made to
the function parameter will not change the argument.
int main( )
{
int n
int n = 3;
myFunction(n); 3
cout << n;
...
}
void myFunction (int n) int n
{ 4
++n;
cout << n << endl; local variable n 48
} is incremented
PASSING ARGUMENTS BY VALUE (CONT.)
When passing arguments by value, any changes made to
the function parameter will not change the argument.
int main( )
{
int n
int n = 3;
myFunction(n); 3
cout << n; OUTPUT:
...
} 4
void myFunction (int n) int n
{ 4
++n;
cout << n << endl; cout statement 49
} is executed
PASSING ARGUMENTS BY VALUE (CONT.)
When passing arguments by value, any changes made to
the function parameter will not change the argument.
int main( )
{
int n
int n = 3;
myFunction(n); 3
cout << n; OUTPUT:
...
} 4
void myFunction (int n)
{ function execution is
terminated and all local
++n;
variables are destroyed
cout << n << endl; 50
}
PASSING ARGUMENTS BY VALUE (CONT.)
When passing arguments by value, any changes made to
the function parameter will not change the argument.
int main( )
{
int n
int n = 3;
myFunction(n); 3
cout << n; OUTPUT:
... return to function call
and print n again 4
}
3
void myFunction (int n)
{
++n;
cout << n << endl; 51
}
PASSING ARGUMENTS BY REFERENCE
When passing by reference
Typically used
For input function to retrieve data for caller,
data is then "given" to caller
When more than one value needs to be returned
int main( )
{
int n = 3;
myFunction(n);
cout << n;
...
}
void myFunction (int& n)
{
++n;
cout << n << endl; 53
}
PASSING ARGUMENTS BY REFERENCE (CONT.)
When passing arguments by reference, any changes made
to the function parameter will change the argument.
int main( )
{ int n
int n = 3; 3
myFunction(n);
cout << n;
...
}
void myFunction (int& n)
{
++n;
cout << n << endl; 54
}
PASSING ARGUMENTS BY REFERENCE (CONT.)
When passing arguments by reference, any changes made
to the function parameter will change the argument.
int main( )
{ int n
int n = 3; 3
myFunction(n);
cout << n; call to myFunction (3)
...
}
void myFunction (int& n)
{
++n;
cout << n << endl; 55
}
PASSING ARGUMENTS BY REFERENCE (CONT.)
When passing arguments by reference, any changes made
to the function parameter will change the argument.
int main( )
{ int n
int n = 3; 3
myFunction(n);
cout << n;
...
address of n is passed
}
void myFunction (int& n) int& n
{
[address]
++n;
cout << n << endl; 56
}
PASSING ARGUMENTS BY REFERENCE (CONT.)
When passing arguments by reference, any changes made
to the function parameter will change the argument.
int main( )
{ int n
int n = 3; 4 increments
myFunction(n); variable at
cout << n; address
...
finds the
}
address in
void myFunction (int& n) int& n local scope
{
[address]
++n;
cout << n << endl; increment n 57
}
PASSING ARGUMENTS BY REFERENCE (CONT.)
When passing arguments by reference, any changes made
to the function parameter will change the argument.
int main( )
{ int n
int n = 3; 4
myFunction(n);
cout << n; OUTPUT:
...
} 4
void myFunction (int& n) int& n
{
[address]
++n;
cout << n << endl; cout statement is executed 58
}
PASSING ARGUMENTS BY REFERENCE (CONT.)
When passing arguments by reference, any changes made
to the function parameter will change the argument.
int main( )
{ int n
int n = 3; 4
myFunction(n);
cout << n; OUTPUT:
...
} 4
void myFunction (int& n)
{ function execution is
++n; terminated and all local
cout << n << endl; variables are destroyed
59
}
PASSING ARGUMENTS BY REFERENCE (CONT.)
When passing arguments by reference, any changes made
to the function parameter will change the argument.
int main( )
{ int n
int n = 3; 4
myFunction(n);
cout << n; return to function call OUTPUT:
... and print n again
} 4
4
void myFunction (int& n)
{
++n;
cout << n << endl; 60
}
EXAMPLE
Project: parameter_passing
61
FUNCTION OVERLOADING
Overloaded functions have
Same function name
Different parameter lists
Two separate function declarations/definitions
Function "signature"
Function name & parameter list
Must be "unique" for each function definition
62
FUNCTION OVERLOADING (CONT.)
Example:
...
int score[CAPACITY];
66
VECTOR SIZE
The STL vector class defines size as the
number of elements stored in the vector.
If using a loop, avoid calling the function size
inside the loop and use a variable instead
69
WHEN TO PASS BY REFERENCE (&)?
When passing objects
They are large; no need to make another copy
Example: strings, vectors, objects of classes you
created
70
WHEN TO PASS BY REFERENCE? (CONT.)
When passing variables that need to be changed and
retain their new value after the function is done
71
WHEN TO USE const?
IF you are passing by reference (&)
AND the value passed by the parameter
should not be modified inside the function
THEN use const
72
PASSING ARRAYS
Careful! Arrays are automatically passed by
reference, but no & is used!
Need to use const when necessary
73
EXAMPLES
Project: arrays
Project: vectors
74
GOOD PROGRAMMING
75
CHANGING FLOW OF CONTROL
IMPORTANT
Do NOT use:
76
A FEW RULES
When creating a new VS project
Name your project “Project”
You should rename the folder later
If the project name is too long, files might not be transfer
77
A FEW RULES (CONT.)
When coding:
Leave a space in between operators
Leave a line in between blocks of code
Split statements to avoid horizontal scrolling
Improve readability when writing decimal numbers:
0.0 instead of .0
3.0 instead of 3
Write code that is easy to read and understand
You are not going to look “cool” if you write some code that
is difficult to read
Declare variables only right before you need them,
instead of listing them at the beginning of the 78
function
ARE YOU DETAIL ORIENTED?
As a programmer, you need to:
79
END REVIEW 1
80