Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Shahan03

  • 3 years ago

class C{ public: int f(int a){return x*a;} protected: int getX(){return x;} private: int x; }; cout<<objC.getX(); //ERROR : C::getX is //unaccessible Why?

  • This Question is Closed
  1. atlas
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I am not really into programming.......but it seems ok to me

  2. thangeram
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it has many errors

  3. thangeram
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    parameters are not properly used in programms

  4. atlas
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @thageram I believe he did not write the complete program to save tedious typing

  5. atlas
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @thangeram

  6. REMAINDER
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    are u calling it in main or in the implementation file

  7. eszeikial
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Int getX needs to be public. not protected.

  8. NeThWa
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    The protected-flag only allows the Object itself and its following generations (classes, which extend this class, inheritance) to access the method. I'm not familiar with C (i guess its C) but for me it looks like thats your problem. Change it to public and it maybe it'll work.

  9. sonymol
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    if any data member or member function is declared protected they will be accessible only in the class itself and the inherited class .Further accessibility will depend on the type of inheritance i.e public ,private etc.. To sort this out just make int getX() function public . I suppose that the function cout<<objC.getX(); is written in the main() or in a member function after creating objC .

  10. thangeram
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    the value of getx is protected in this program there is parameters x is used to pass the value of x but the value of x is private so main cannot execute it he should change it public

  11. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy