C++ Operator Overloading Question:
Download Questions PDF

What is the output of this program?

#include <iostream>
using namespace std;
class sample1
{
float i, j;
};
class sample2
{
int x, y;
public:
sample2 (int a, int b)
{
x = a;
y = b;
}
int result()
{
return x + y;
}
};
int main ()
{
sample1 d;
sample2 * padd;
padd = (sample2*) &d;
cout < result();
return 0;
}
a) 20
b) runtime error
c) random number
d) c or b

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

Answer:

d) c or b

Download C++ Operator Overloading Interview Questions And Answers PDF

Previous QuestionNext Question
How many parameters does a conversion operator may take?
a) 0
b) 1
c) 2
d) as many as possible
program Output:

#include <iostream>
using namespace std;
class sample
{
public:
sample(int i) : m_i(i) { }
public:
int operator()(int i = 0) const
{
return m_i + i;
}
operator int () const
{
return m_i;
}
private:
int m_i;
friend int g(const sample&);
};
int f(char c)
{
return c;
}
int main()
{
sample f(2);
cout << f(2);
return 0;
}
a) 3
b) 4
c) 5
d) None of the mentioned