C++ Operator Overloading Question:

Download Job Interview Questions and Answers PDF

Output of Program?

#include <iostream>
using namespace std;
class sample
{
public:
int x, y;
sample() {};
sample(int, int);
sample operator + (sample);
};
sample::sample (int a, int b)
{
x = a;
y = b;
}
sample sample::operator+ (sample param)
{
sample temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main ()
{
sample a (4,1);
sample b (3,2);
sample c;
c = a + b;
cout << c.x << "," << c.y;
return 0;
}
a) 5, 5
b) 7, 3
c) 3, 7
d) None of the mentioned

C++ Operator Overloading Interview Question
C++ Operator Overloading Interview Question

Answer:

b) 7, 3

Download C++ Operator Overloading Interview Questions And Answers PDF

Previous QuestionNext Question
Which of the following operators can't be overloaded?
a) ::
b) +
c) -
d) []
Output of this program?

#include <iostream>
using namespace std;
class Integer
{
int i;
public:
Integer(int ii) : i(ii) {}
const Integer
operator+(const Integer& rv) const
{
cout << "operator+" << endl;
return Integer(i + rv.i);
}
Integer&
operator+=(const Integer& rv)
{
cout << "operator+=" << endl;
i += rv.i;
return *this;
}
};
int main()
{
int i = 1, j = 2, k = 3;
k += i + j;
Integer ii(1), jj(2), kk(3);
kk += ii + jj;
}
a) operator+
operator+=
b) operator+=
operator+
c) operator+
operator+
d) None of the mentioned