CS253: Software Development with C++

Spring 2020

IQ 09

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?

  1. auto_ptr
  2. shared_ptr
  3. stack_ptr
  4. unique_ptr
  5. 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?

  1. auto_ptr
  2. shared_ptr
  3. stack_ptr
  4. unique_ptr
  5. 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?

  1. #include <string.h>
  2. #include <string>
  3. #include <cstring>
  4. #include <ᵷuᴉɹʇs>
  5. 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?

  1. char v; if (cin >> v) …
  2. char v; if (cin.get(v)) …
  3. int v; if (cin >> v) …
  4. string v; if (getline(cin, v)) …
  5. string v; if (cin >> v) …

  1. 2
  2. 3
  3. 2 and 4
  4. 1, 3, and 5
  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?

  1. +0123.
  2. +0x7B.
  3. 53
  4. 0X53
  5. 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.