Ch. 3: The C in C++ - Continued -
QUIZ What are the 3 ways a reference can be passed to a C++ function?
QUIZ True or false: References behave like constant pointers with automatic dereferencing.
QUIZ What are 3 differences between references and pointers?
Someone says: The very example used in the text to illustrate references shows that a reference does have an address!
What s wrong with this swap() function? What will be printed?
Specifying storage allocation
Ignore the author s special comments any decent IDE will offer better ways to manage separate compilation and linking.
Static illustrates the difference between scope and lifetime of objects!
What would the output be if static were omitted?
Can we refer to i in main() or other functions?
Can we refer to i in main() or other functions? No! Although I still exists, it is out of scope anywhere outside func()!
Types of linkage variables can have Internal (static) the name can be reused in other files External (extern) the name cannot be reused in other files No linkage, i.e. the linker does not know about these variables (auto)
Constants Both C and C++ allow preprocessor constants (although their use is discouraged in C++): Downsides: No type checking No possibility to control their scope (they have file scope) Cannot access their address
Constants Both C and C++ allow constant variables (they existed in C++ from the beginning 1983 -, but they were introduced in C only in C89): In C++, a const must always have an initialization value (in C, this is not true)
What the author means to say is that in C const variables have external linkage, whereas in C++ they have internal linkage.
Another difference between C and C++ const variables In C++ const variables must be initialized, whereas in C they can be uninitialized.
Individual work for next time: End-of-chapter problems #9 and #10. EOL 9
QUIZ If a variable is declared static in C or C++, what is its: lifetime? scope?
QUIZ If a variable is declared static in C or C++, what is its: lifetime? The lifetime of the entire program scope? The block in which it was declared. If declared global, it has file scope (i.e. it cannot be accessed in a different file!)
QUIZ What are 2 differences between C and C++ const variables?
QUIZ What are 2 differences between C and C++ const variables? In C const variables have external linkage, whereas in C++ they have internal linkage. In C const variables may be uninitialized, whereas in C++ they must be initialized.
QUIZ What is the meaing of the keyword volatile in C and C++? Why is it needed?
Operators and their use This section is largely meant for reading and reviewing. Our lecture coverage will focus on the features that are either tricky, or new in C++, compared to C.
Preprocessor macros......
Preprocessor macros Warning: Macros are considered unsafe, and their use is discouraged! They have no scope, rather they are processed by the preprocessor from top to bottom in the source code, irrespective in which blocks or functions they appear. This can have unintuitive effects. Consider using functions instead! (More details in Ch.9)
Preprocessor macros We shall not require macro programming in this class! (Actually, we re taking points off for macros )
Not a good idea to compare floating point numbers for equality!
Not a good idea to compare floating point numbers for equality! What to do?
Example application for shift operator Explain what happens in binary!
Even though most computer CPUs provide a machinelevel rotate command (so you ll see it in the assembly language for that processor), there is no direct support for rotate in C or C++. Presumably the designers of C felt justified in leaving rotate off (aiming, as they said, for a minimal language) because you can build your own rotate command.
Explain what happens in binary!
Casting operators The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integral value to a floating-point variable, the compiler will secretly call a function (or more probably, insert code) to convert the int to a float. Casting allows you to make this type conversion explicit, or to force it when it wouldn t normally happen.
C++ explicit casts
static_cast The author s machine has 16-bit int
const_cast
reinterpret_cast
...
Composite type creation This section is also largely meant for reading and reviewing. Our lecture coverage will focus on the features that are either tricky, or new in C++, compared to C.
Aliasing names with typedef Starting with C++11, the keyword using is a better alternative to typedef!
What is the problem with typedef? A small but annoying practical problem: typedef s syntax is in reverse from define s
What is the problem with typedef? In the greater scheme of C++ things, using allows template typedef (later), so it is more general.
QUIZ: Rewrite with using instead of typedef Optional (and misleading in this example, since the new type is also called Structure3)
QUIZ: Rewrite with using instead of typedef
The following topics will be covered in the lab: Arguments of main(): argv[] and argc Complicated declarations & definitions
SKIP: Debugging hints Arrays of pointers to functions Make: managing separate compilation
The remainder of Ch.3 is assigned for reading and reviewing: Structures Unions Arrays (and pointers, pointer arithmetic) Enumerations Function addresses and function pointers
Homework for ch. 3 Provided as separate handout (also available on our webpage --> agapie.net) Due Friday, Sept. 25, at the beginning of class. Please hand in a hard-copy, do not email! EOL 10