CS253: Software Development with C++

Spring 2020

IQ 05

CS253 IQ 05

Show Main.IQ05 as a slide show.

What will the midterm cover?

  1. Just the lectures
  2. Just the labs
  3. Just the homework
  4. All of the above

ctor

Which of these is a reasonable ctor for a rational number class?

  1. Rat(int a, int b) { }
  2. Rat(int a_, int b_) { a = a_; b = b_; }
  3. Rat Rat(int a_arg, int b_arg) : a(a_arg), b(b_arg) { }
  4. void Rat(int a, int b) { }
  5. void Rat(int aVal, int bVal) { (a,b) = (aVal,bVal); }

Is this valid code?

int main(int argc, char *argv[]) {
    constexpr int num_args = argc;
}
  1. Yes—argc will be “captured”, and constant from then on.
  2. Yes, because argc is an argument to the function.
  3. No, because argc isn’t known until run-time.
  4. No, but const constexpr numargs = argc would be ok.
  5. Only if the number of arguments never changes.

const

Is the compiler allowed to assume that the if statement will always be true?

void foo(const string &name) {
    string me = name;
    bar();
    if (me == name)
        cout << "equal\n";
}
  1. Yes
  2. No

operator overloading

Which of these is the best way to make a+b work, where a and b are class Foo?

  1. Foo &operator+(Foo &);
  2. Foo &operator+(Foo);
  3. Foo &operator+(Foo, Foo);
  4. Foo operator+(const Foo &) const;
  5. Foo operator+(const Foo &, const Foo &) const;