CS253: Software Development with C++

Fall 2018

IQ 13

CS253 IQ 13

See this page as a slide show

Any problems with this code?

class Times {
    const int factor;
  public:
    Times(int f) : factor(f) { }
    int operator()(int n) { return n*factor; }
};

Times f(3);
cout << f(10) << ' ' << f(12); // expect 30 36
30 36
  1. factor is neither public nor private.
  2. factor shouldn’t be const, since it gets assigned.
  3. operator() should be const.
  4. The ctor should use auto f instead of int f for increased generality.
  5. What fine code!

What will this code produce?

const char *cap = "James T. Kirk";
auto v = find(begin(cap), end(cap), 'T');
cout << v;
c.cc:2: error: no matching function for call to 'begin(const char*&)'

  1. true
  2. T
  3. T. Kirk
  4. some hexadecimal pointer value
  5. none of the above

E: end() isn’t defined for a pointer

What will this different code produce?

const char cap[] = "James T. Kirk";
auto v = find(begin(cap), end(cap), 'T');
cout << v;
T. Kirk

  1. true
  2. T
  3. T. Kirk
  4. some hexadecimal pointer value
  5. none of the above

E: v is a const char *. It’s a C-style string—display chars until a null characters ('\0') is found.

What will this code produce?

int a[] = {11,22,33,44,55,66};
auto v = find(a, a+5, 42);
cout << v;
0x7ffdd94f4c64
  1. false
  2. nullptr
  3. 66
  4. some hexadecimal pointer value
  5. none of the above

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-11-14T21:59

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building