Home » C++ Programming » Classes & Objects » Question
  1. What is the output of this program?
    #include 
    using namespace std;
    class Example
    {
    private:
    int num;
    public:
    void input()
    {
    cout << num;
    }
    void Result()
    {
    cout << "Entered Variable is: ";
    cout << num << "\n";
    }
    };
    int main()
    {
    Example obj;
    obj.input();
    obj.Result();
    obj.num();
    return 0;
    }
    1. Runtime Error
    2. Garbage value
    3. Compilation Error
    4. Entered Variable is: 15
    5. None of these
Correct Option: C

While using private member, you can’t access it variable.
Compilation Error

In function 'int main()':
6:13: error: 'int Example::num' is private
23:13: error: within this context
23:17: error: expression cannot be used as a function



Your comments will be displayed only after manual approval.