Home » C++ Programming » Operators » Question
  1. What is the output of this program?
    #include <iostream> 
    using namespace std;
    class Example
    {
    public:
    int p;
    Example(int num = 0) : p(num) {};
    int& operator[](int num)
    {
    cout << "Interview " ;
    return p;
    }
    int operator[](int num) const
    {
    cout << "Mania" ;
    return p;
    }
    };
    void foo(const Example& n)
    {
    int Res = n[2];
    }
    int main()
    {
    Example n(7);
    n[3] = 8;
    int Res = n[2];
    foo(n);
    return 0;
    }
    1. Interview Interview Mania
    2. Interview Mania
    3. Mania
    4. Compilation Error
    5. None of these
Correct Option: A

In this program, we overloading the operator[] by using subscript operator.



Your comments will be displayed only after manual approval.