ProgrammingHardProduct-company placement · GATE CS

What is printed?

What is printed?
struct S {
    void f()       { cout << "N"; }
    void f() const { cout << "C"; }
};
int main() {
    S a;
    const S b{};
    const S& r = a;
    S* p = &a;
    a.f(); b.f(); r.f(); p->f();
}
  1. A. NCNN
  2. B. NCCN
  3. C. NNNN
  4. D. NCCC

Correct answer: B. NCCN

Overloading on const uses the static type of the object expression. r refers to a non-const object, but through a const S& only the const member can be called. p is a pointer to non-const S, so it gets the non-const version.

More OOP with C++ and Java questions

Exams that ask this topic