Which of the statements are true? I. Function overloading is done at compile…
2009
Which of the statements are true?
I. Function overloading is done at compile time.
II. Protected members are accessible to the members of a derived class.
III. A derived class inherits constructors and destructors.
IV. A friend function can be called like a normal function.
V. A nested class is a derived class.
Answer: D. I, II, IV — CONCEPT: Five independent C++ language rules govern this item: (1) overload resolution among same-named functions is performed by the compiler from the…
- A.
I, II, III
- B.
II, III, V
- C.
III, IV, V
- D.
I, II, IV
Attempted by 34 students.
Show answer & explanation
Correct answer: D
CONCEPT: Five independent C++ language rules govern this item: (1) overload resolution among same-named functions is performed by the compiler from the argument-list signature, making it a compile-time (static) mechanism; (2) a protected member is accessible to the members of any class derived from its declaring class, in addition to the declaring class itself; (3) constructors and destructors are not inherited automatically -- a derived class must define its own (or rely on the compiler-generated ones); C++11 does allow a derived class to explicitly opt in to inheriting a base class's constructors via a using-declaration (e.g. using Base::Base;), but destructors are never inherited under any mechanism, and no such automatic/default inheritance of either exists; (4) a friend function is an ordinary non-member function granted special access to a class, so it is invoked with normal function-call syntax like any other free function; and (5) a nested class only lives inside the scope of an enclosing class -- nesting is a scoping relationship, not an inheritance (derived-class) relationship.
Application
Statement I -- overload resolution for functions with the same name is done by the compiler at compile time based on the argument list, so this statement holds.
Statement II -- a protected member of a base class is accessible to the members of a class derived from it, so this statement holds.
Statement III -- read as the general/default claim the statement makes, a derived class does not automatically receive the base class's constructor or destructor; C++11's optional using-declaration mechanism for inheriting constructors is an explicit opt-in, not automatic inheritance, and destructors are never inherited by any mechanism, so this statement fails as stated.
Statement IV -- a friend function is a normal (non-member) function with special access rights; it is called exactly like any other ordinary function, so this statement holds.
Statement V -- defining a class inside another class only creates a nested scope; it does not establish a base-derived (inheritance) relationship, so this statement fails.
CROSS-CHECK: Statements I, II, and IV hold while III and V fail, so the only fully correct combination is I, II, IV. Every other offered combination pairs at least one of the failing statements (III or V) with the holding ones, which rules it out -- confirming I, II, IV as the unique consistent combination.