Open In App
Related Articles

C++ | Operator Overloading | Question 3

Improve Article
Improve
Save Article
Save
Like Article
Like

Which of the following operators are overloaded by default by the compiler in every user defined classes even if user has not written?

1) Comparison Operator ( == )
2) Assignment Operator ( = ) 

(A) Both 1 and 2
(B) Only 1
(C) Only 2
(D) None of the two


Answer: (C)

Explanation: Assign operator is by default available in all user defined classes even if user has not implemented. The default assignement does shallow copy.

But comparison operator “==” is not overloaded.

#include<iostream>
using namespace std;

class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i =0)  {real = r;   imag = i;}
};

int main()
{
    Complex c1(10, 5), c2(2, 4);

    // For example, below code works fine
    c1 = c2;

    // But this code throws compiler error
    if (c1 == c2)
       cout << "Same";

    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 : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Similar Reads