C++ | Operator Overloading | Question 5
How does C++ compiler differs between overloaded postfix and prefix operators?
(A) C++ doesn’t allow both operators to be overlaoded in a class
(B) A postfix ++ has a dummy parameter
(C) A prefix ++ has a dummy parameter
(D) By making prefix ++ as a global function and postfix as a member function.
Answer: (B)
Explanation: See the following example:
class Complex
{
private:
int real;
int imag;
public:
Complex(int r, int i) { real = r; imag = i; }
Complex operator ++(int);
Complex & operator ++();
};
Complex &Complex::operator ++()
{
real++; imag++;
return *this;
}
Complex Complex::operator ++(int i)
{
Complex c1(real, imag);
real++; imag++;
return c1;
}
int main()
{
Complex c1(10, 15);
c1++;
++c1;
return 0;
}
Recommended Posts:
- C++ | Operator Overloading | Question 3
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 9
- C++ | Operator Overloading | Question 4
- C++ | Operator Overloading | Question 2
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 7
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 6
- C++ program to compare two Strings using Operator Overloading
- C++ Program to concatenate two strings using Operator Overloading
- C++ | Function Overloading and Default Arguments | Question 4
- C++ | Function Overloading and Default Arguments | Question 5
- C++ | Function Overloading and Default Arguments | Question 3
- C++ | Function Overloading and Default Arguments | Question 2



