Examveda

What will be the output of the following C++ code?
#include <iostream> 
using namespace std;
class A
{
    public:
    int x;
    A(int n = 0) : x(n) {};
    int& operator[](int n)
    {
         cout << "0" ;
         return x;
    }
    int operator[](int n) const
    {
         cout << "1" ;
         return x;
    }
 };
void foo(const A& a)
{
    int z = a[2];
}
int main()
{
    A a(7);
    a[3]  = 8;
    int z = a[2];
    foo(a);
    return 0;
}

A. 110

B. 111

C. 011

D. 001

Answer: Option D


Join The Discussion

Related Questions on Classes and Objects in C plus plus