CS253: Software Development with C++

Spring 2020

IQ 04

CS253 IQ 04

Show Main.IQ04 as a slide show.

What will this code display?

auto foo = "123456789";
cout << foo.size();
c.cc:2: error: request for member 'size' in 'foo', which is of non-class type 
   'const char*'
  1. 4
  2. 8
  3. 9
  4. 10
  5. None of the above.

What will this different code display?

auto foo = "123456789";
cout << sizeof(foo);
8
  1. 4
  2. 8
  3. 9
  4. 10
  5. None of the above.

What will this code display?

for (int i=0; i<2; i++) {
    cout << "α ";
    static auto &foo = (cout << "β ");
    foo << "γ ";
}
α β γ α γ 
  1. α β γ α β γ
  2. α β γ α γ
  3. α γ α γ
  4. β α γ α γ
  5. None of the above.

What’s the difference?

  1. struct is for C programs, class for C++ programs.
  2. class can have methods, struct can’t.
  3. struct defaults to public, class defaults to private.
  4. classes are automatically friends, structs are not.
  5. They are exactly the same.

Friends

class Witch wants to access class Vision’s private data.

  1. Put friend Vision Witch; outside of either class.
  2. Put friend Witch Vision; outside of either class.
  3. Put friend class Witch; inside Vision.
  4. Put friend class Vision; inside Witch.
  5. Make Witch a nested class within class Vision.