CS253 IQ 05
Show Main.IQ05 as a slide show.
What will the midterm cover?
- Just the lectures
- Just the labs
- Just the homework
- All of the above
ctor
Which of these is a reasonable ctor for a rational number class?
Rat(int a, int b) { }
Rat(int a_, int b_) { a = a_; b = b_; }
Rat Rat(int a_arg, int b_arg) : a(a_arg), b(b_arg) { }
void Rat(int a, int b) { }
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;
}
- Yes—
argc
will be “captured”, and constant from then on.
- Yes, because
argc
is an argument to the function.
- No, because
argc
isn’t known until run-time.
- No, but
const constexpr numargs = argc
would be ok.
- 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";
}
- Yes
- No
operator overloading
Which of these is the best way to make a+b
work,
where a
and b
are class Foo
?
Foo &operator+(Foo &);
Foo &operator+(Foo);
Foo &operator+(Foo, Foo);
Foo operator+(const Foo &) const;
Foo operator+(const Foo &, const Foo &) const;