In C++ language features, which of the following can be overloaded to provide multiple meanings based on parameter types or counts? Choose the most complete answer.
-
AObject
-
BFunctions
-
COperators
-
DBoth B and C
-
ENamespaces
Answer
Correct Answer: Both B and C
Explanation
Introduction / Context:Overloading allows the same name or symbol to represent different operations depending on argument types or arity. C++ supports two primary forms of overloading: function overloading and operator overloading. Identifying both is fundamental to idiomatic C++.
Given Data / Assumptions:
- Function overloads differ by parameter types, counts, or cv/ref qualifiers.
- Operator overloading is implemented via specially named functions (e.g., operator+).
- Objects and namespaces are not overloadable entities themselves.
Concept / Approach:Function overloading lets you define multiple functions with the same name but different parameter lists. Operator overloading lets you give standard operators (+, -, [], (), etc.) custom behavior for user-defined types while preserving operator syntax. Both are resolved at compile time via overload resolution rules.
Step-by-Step Solution:1) Function: void print(int); void print(std::string const&);2) Operator: struct Vec { Vec operator+(Vec const&) const; };3) Calls like print(42) vs print(s) select the correct overload at compile time.4) Using v1 + v2 invokes Vec::operator+.
Verification / Alternative check:Compile examples; observe that the compiler picks appropriate overloads without runtime cost.
Why Other Options Are Wrong:Object: instances are not “overloaded”.Namespaces: they organize code; their names are not overloadable constructs.
Common Pitfalls:Abusing operator overloading to create confusing semantics; follow established expectations (e.g., + should not mutate in surprising ways).
Final Answer:Both B and C