πŸ“Š C++
Q. An operator function is created using _____________ keyword.
  • (A) iterator
  • (B) operator
  • (C) allocator
  • (D) constructor
πŸ’¬ Discuss
βœ… Correct Answer: (B) operator

Explanation:

In C++, operator overloading allows you to define how operators (like +, -, *, ==, etc.) behave for objects of your class. To create an operator function, you use the operator keyword.

Example:

class Complex {
public:
int real, imag;

Complex(int r, int i) : real(r), imag(i) {}

// Overload the + operator
Complex operator+(const Complex& other) {
return Complex(real + other.real, imag + other.imag);
}
};

Here, operator+ is the operator function.

βœ… Correct Answer: (B) operator

Explanation by: Mr. Dubey

In C++, operator overloading allows you to define how operators (like +, -, *, ==, etc.) behave for objects of your class. To create an operator function, you use the operator keyword.

Example:

class Complex {
public:
int real, imag;

Complex(int r, int i) : real(r), imag(i) {}

// Overload the + operator
Complex operator+(const Complex& other) {
return Complex(real + other.real, imag + other.imag);
}
};

Here, operator+ is the operator function.

βœ… Correct Answer: (B) operator

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
234
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Ram Sharma
Publisher
πŸ“ˆ
81%
Success Rate