Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Opeartor ++ for complex Write a method to implement operator++ for the Complex c

ID: 3783613 • Letter: O

Question

Opeartor++ for complex

Write a method to implement operator++ for the Complex class. This method should result in the number having one added to the real component and return the new value of the number. This is the prefix version of the operator, so it would be used like this: Complex b = ++a; Write the code that would be in the cpp file. The header file contains: Complex &operator;++ ();//increments a Complex number by one FYI: The postfix version is similar, but uses a dummy ml parameter to distinguish itself from the prefix version. The postfix version also returns by value, instead of reference.

Explanation / Answer

Hi Lets assume that 'real' and 'imag' are two fields of Complex class and there is parameterised constructor:

Complex(int r, int img)

Then implementation of ++ function:


Complex &operator++(){
   Complex inc(real+1, imag);
   return inc;
}