- 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 correct answer is option (c). The + operator can be overloaded in C++. Operators such as ::, sizeof, and . cannot be overloaded.
- How do we overload the prefix increment operator ++ for a class?
class A {
public:
A& operator++();
};
- A operator++(int);
- A& operator++;
- A& operator++(int);
- void operator++;
Explanation:
The correct answer is option (b). The prefix increment operator ++ is overloaded as a member function that returns a reference to the class type.
- What is the correct way to declare a friend function to overload the binary + operator in C++?
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 correct answer is option (b). The friend function to overload the binary + operator should be declared with two parameters passed by constant reference.
- Which of the following is a correct example for function overloading?
void print(int i);
void print(double f);
- Both
- None of the above
Explanation:
The correct answer is option (a). The functions print(int) and print(double) have equal names. However, they have different parameter types, which is a valid instance of function overloading.