- What is the motive for overloading the subscript operator in a class?
- To access class members using index notation.
- To perform arithmetic operations.
- To concatenate strings.
- To overload assignments.
Explanation:
The correct answer is option (a). The subscript operator is typically overloaded to permit entry to elements of a container class using index notation.
- Which of the following statement is correct about the overloading of the function call operator ?
- It cannot be overloaded.
- It must be a friend function.
- It allows objects to be used as functions.
- It can only be overloaded in derived classes.
Explanation:
The correct answer is option (c). The function call operator can be overloaded to allow objects to be used even though they were features.
- Which of the following operators can be overloaded in C++?
- sizeof
- . (dot operator)
Explanation:
The accurate choice is alternative (c). In C++, the + operator is capable of being overloaded. Operators like ::, sizeof, and . are not overloadable.
- What is the process for overloading the prefix increment operator ++ in a class?
class A {
public:
A& operator++();
};
- A operator++(int);
- A& operator++;
- A& operator++(int);
- void operator++;
Explanation:
The accurate choice is alternative (b). The prefix increment operator ++ is redefined as a member function that provides a reference to the class type.
How should a friend function be declared in C++ to overload the binary + operator correctly?
class A {
int value;
public:
A(int v) : value(v) {}
friend A operator+(const A&, const A&);
};
- A operator+(A, A);
- A operator+(const A&, const A&);
- A operator+(A&);
- A operator+(const A&);
Explanation:
The accurate choice is alternative (b). The auxiliary function for overloading the addition operator should be defined with two parameters passed by constant reference.
- Which of the subsequent instances demonstrates proper function overloading?
void print(int i);
void print(double f);
- Both
- None of the above
Explanation:
Option (a) is the accurate choice. The print(int) and print(double) functions share identical names but differ in parameter types, exemplifying function overloading.