CS253 IQ 09
Show Main.IQ09 as a slide show.
Smart Pointers
void foo() {
Zulu<double> p = new double;
…
// use p here
…
// expect the memory to be freed
}
Instead of Zulu
, which one of these would be best?
auto_ptr
shared_ptr
stack_ptr
unique_ptr
- None of these would work.
Trick question—smart pointers have explicit
ctors, so you need to
use the parenthetical ctor notation. This won’t compile.
Smart Pointers
void foo() {
Zulu<double> p(new double);
…
// use p here
…
// expect the memory to be freed
}
Instead of Zulu
, which one of these would be best?
auto_ptr
shared_ptr
stack_ptr
unique_ptr
- This won’t compile, either.
auto_ptr
is obsolete.
stack_ptr
doesn’t exist.
shared_ptr
would work, but it has overhead.
unique_ptr
is best.
Namespaces
Which of these puts the symbol string
into the global namespace?
#include <string.h>
#include <string>
#include <cstring>
#include <ᵷuᴉɹʇs>
- none of the above
<string.h>
& <cstring>
are for C functions, strlen()
, etc.
<string>
puts string
in the std::
namespace.
<ᵷuᴉɹʇs>
defines class ᵷuᴉɹʇs
.
I/O
Which of these will skip whitespace before the value?
-
char v; if (cin >> v) …
char v; if (cin.get(v)) …
int v; if (cin >> v) …
string v; if (getline(cin, v)) …
string v; if (cin >> v) …
- 2
- 3
- 2 and 4
- 1, 3, and 5
- they all will
>>
, formatted input, skips leading whitespace
(unless noskipws
was used).
I/O Manipulators
cout << hex
<< right
<< showbase
<< showpoint
<< showpos
<< uppercase
<< 0123
<< endl;
0X53
What will this code produce?
- +0123.
- +0x7B.
- 53
- 0X53
- 0123
1238 is 5316.
showbase
adds 0x
, which uppercase
makes 0X
.
showpos
has no effect, because hex
treats numbers as unsigned.
showpoint
only applies to floating-point numbers.