Open In App
Related Articles

C++ | Operator Overloading | Question 5

Improve Article
Save Article
Like Article

How does C++ compiler differs between overloaded postfix and prefix operators?
(A) C++ doesn’t allow both operators to be overloaded 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
    int real;
    int imag;
    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);
    return 0;

Quiz of this Question

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 16 Apr, 2021
Like Article
Save Article
Similar Reads