Function Overloading in C++
15 questions · 12 min · Medium
Test your knowledge of operator overloading in C++, including overloaded operators, member and friend functions, unary and binary operators, operator restrictions, and practical examples of operator overloading.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
B. Giving an existing operator a specific meaning for user-defined types
Operator overloading allows existing C++ operators to work with user-defined types, such as classes and structures.
A. operator
The operator keyword introduces an operator function, such as operator+ or operator==.
C. Addition operator (+)
The addition operator can be overloaded to define how values of a user-defined type should be added.
D. ::
The scope resolution operator (::) cannot be overloaded in C++.
B. ==
Overloading operator== allows a class to define how two objects should be compared for equality.
C. Define an operator function that takes one explicit operand parameter
For a binary operator overloaded as a non-static member function, the left operand is the calling object and the right operand is passed as a parameter.
A. Two
A non-member binary operator function normally takes two parameters, one for each operand.
D. It can access a class's private and protected members while remaining a non-member function
A friend function is not a member of the class, but the class can grant it access to private and protected members.
B. It cannot change an operator's precedence or associativity
Operator overloading changes the behavior for user-defined types but does not change an operator's precedence, associativity, or number of operands.
C. operator++()
The operator++ function overloads increment. Prefix and postfix forms use different parameter conventions when implemented as member functions.
A. The postfix version takes an unused int parameter
A conventional postfix member overload uses a dummy int parameter to distinguish it from the prefix form, which takes no explicit parameter.
D. operator[]
Overloading operator[] allows objects such as custom containers to support indexed access using square brackets.
B. operator()
Overloading operator() allows an object to be invoked using function-call syntax, which is useful for function objects.
C. It invokes the applicable overloaded addition operator
The expression uses the overloaded operator+ function selected by the compiler to define addition for Complex objects.
A. A class can define a copy assignment operator to customize assignment between objects
A class can provide a copy assignment operator to control how one object is assigned to another. If suitable special member functions are not user-declared, the compiler may implicitly declare and define one.