QUIZ 1. Explain the meaning of the angle brackets in the declaration of v below:
This is a template, used for generic programming!
QUIZ 2. Why is the vector class called a container? 3. Explain how the C++ vectors and the C/C++ arrays are: a. Similar b. Different 4. How can we add elements to a vector at runtime?
This is a template, used for generic programming!
Ch. 3: The C in C++
Creating functions
Creating functions Argument names are optional in the prototype (declaration).
Function return values
A function can have more than one return statement
Using the C function library
Using the C function library The difference between #include <header> and #include "header"
SKIP Creating your own libraries with the librarian (It is specific to the IDE of programming package we re using)
Controlling execution
QUIZ Write a C++ program that asks the user for an integer, and then displays one of 3 possible messages: Less than 0 Equal to 0 Greater than 0
QUIZ Encapsulate the decision part of the previous program in a function called verdict(). What are the inputs and outputs of this function?
The while loop
Not in text
The while loop
In a loop, the decision (diamond) can be at the beginning or at the end of the loop! Not in text 20
The do while loop
Design decision: while or do while?
The for loop
Not in text
QUIZ: for loop
QUIZ: for loop
Read and take notes: The break and continue keywords switch Using and misusing goto EOL 6
QUIZ Use a while loop to implement the repeated division algorithm. Hint: The algorithm stops when the quotient is zero!
QUIZ: What s wrong in this program?
That s why the example in the text has char instead of string!
Is this switch correct?
It s OK to switch on a char, since char is an 8-bit integer
Would this program compile in C?
Would this program compile in C?
What s wrong in this switch?
switch statement fall-through
QUIZ Give a code example in which goto is acceptable, and explain why!
Recursion base-case, a.k.a. bottoming-out
QUIZ Write a recursive function to implement the repeated division algorithm. Hint: The function should print the digits in correct order!
Introduction to operators Arithmetic operators: + - * / % Unary, binary, ternary Precedence and parentheses Compound assignment: += -= *= /= %= Auto-increment/decrement, pre- and post-
Introduction to data types
Source: http://www.cplusplus.com/reference/climits/
bool, true, & false The Standard C++ bool type can have two states expressed by the built-in constants true (which converts to an integral one) and false (which converts to an integral zero). All three names are keywords. In addition, some language elements have been adapted:
Specifiers Specifiers modify the meanings of the basic built-in types and expand them to a much larger set. There are four specifiers: long, short, signed, and unsigned.
Specifiers long and short modify the maximum and minimum values that a data type will hold. A plain int must be at least the size of a short. The size hierarchy for integral types is: short int int long int long long int All the sizes could conceivably be the same, as long as they satisfy the minimum/maximum value requirements. On a machine with a 64-bit word, for instance, all the data types might be 64 bits. On our machines, the sizes are 16 32 32 64 bit, respectively.
Specifiers The signed and unsigned specifiers tell the compiler how to use the sign bit with integral types and characters (floatingpoint numbers always contain a sign): An unsigned number does not keep track of the sign and thus has an extra bit available, so it can store positive numbers twice as large as the positive numbers that can be stored in a signed number. signed is the default and is only necessary with char; char may or may not default to signed. By specifying signed char, you force the sign bit to be used.
sizeof operator Do not confuse the size of a data type with its limits! sizeof climits and cfloat
Introduction to pointers The cast is done just to avoid displaying the addresses in hex!
Let s figure out which memory areas these addresses are in!
Pointers are variables that store addresses
Declaring and initializing pointers
What can we do with pointers? Access the variables they point to!
QUIZ Create two floats and two pointers to them. Swap the floats using the pointers.
What can we do with pointers? Access the variables they point to! These are the basics of pointers: you can hold an address, and you can use that address to modify the original variable. But the question still remains: why do you want to modify one variable using another variable as a proxy?
Alfred Hitchcock's Saboteur (1942) To be continued
Read and take notes: Specifying storage allocation all subsections Operators and their use Stop before C++ explicit casts EOL 7
What can we do with pointers? Access the variables they point to! These are the basics of pointers: you can hold an address, and you can use that address to modify the original variable. But the question still remains: why do you want to modify one variable using another variable as a proxy?
We want to be able to modify an outside object from inside a function
Passing function arguments by value
Solution: Passing function arguments by reference pointer (address), using pointers (addresses)
QUIZ Write a C++ program with a function that swaps its two arguments, passed by pointer (address).
QUIZ Write a C++ program with a function that swaps its two arguments, passed by pointer (address).
Introduction to C++ references Accessing the value referenced is simpler! Not *r! Passing the parameter is simpler! Not &x!
QUIZ Write a C++ program with a function that swaps its two arguments, passed by reference.
Conclusion: In C++, function arguments can be passed by: Value (copy) Pointer (address) Reference
Pointer to void
Remember that malloc() returns a pointer to void! Not in text
Once you assign to a void* you lose any information about what type it is. This means that before you can use the pointer, you must cast it to the correct type:
Scoping
Useless!
Although it s permitted by C++, declaring variables in while and switch is awkward
The problem is caused by the precedence of C++ operators
C++ syntax does not allow more than one level of parentheses around declarations!
Individual work for next time: Solve end-of-chapter problem #7. EOL 8