0

I am learning OOPS in C++. I try to access the class data member using pointer. But it throws error.

#include <iostream>

using namespace std;

class A

{
    protected:
        int x;
    public:
        A()
        {
            x=10;
        }
};

class B:public A
{
   public:
   void out()
   {
       A *p=new A;
       cout<<p->x;
   }
};

int main()
{
   B b;
   b.out();
   return 0;
}

The above code gives error as

error: ‘int A::x’ is protected within this context
    |        cout<<p->x;

Can anyone explain why the error occurs? The expected output is to print the value of x. Thanks in advance.

5
  • 1
    The expected output is to print the value of x. Why is it expected? Commented Jul 6, 2023 at 5:46
  • 3
    You create a new object in the out function. That object is unrelated to the this object. You can only access protected members of this object. Commented Jul 6, 2023 at 5:46
  • Also, remember that in C++ you don't have to (and rarely should) use new to create objects. By using new you have a memory leak. Commented Jul 6, 2023 at 5:47
  • Also please note that objects should rarely be just containers of values. Try to model your classes and inheritance hierarchy on behavior. Commented Jul 6, 2023 at 5:50
  • This is a more basic error, so not a direct duplicate of stackoverflow.com/questions/13723217/…. But the answer given there applies here as well. B::out can access the protected member of its base class A sub-object, but not that of an unrelated *p object. Commented Jul 6, 2023 at 13:24

1 Answer 1

0

The problem in this code is related to pointers and making class objects. In the void out() function of class B, you used dynamic memory allocation (i.e. new A) to create an object of class A, and then tried to access the protected 'x' member of class A. Since B is an inherited class, it can access the x member. However, since the object you created in class B (new A) is of class type A, the protected member is not accessible outside the class. So it ends up showing a compilation error. The problem is also that the out() function creates a new A object and it prints the value of x from this new object, not from the B object.

To fix this, you should directly access the x member from the object of class B, as it is derived from class A and can access its protected members. Here's the code:

#include <iostream>

using namespace std;

class A
{
protected:
    int x;

public:
    A()
    {
        x = 10;
    }
};

class B : public A
{
public:
    void out()
    {
        // Access the 'x' member of the current 'B' object
        cout << x;
    }
};

int main()
{
    B b;
    b.out();
    return 0;
}

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.