Thread: Protected and Public

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    66

    Protected and Public

    I'm getting confused with the two. When should I use protected and when should I not? My book says almost all the variables in a class should be protected, yet I found that really cumbersome when doing that. Anyone wanna explain?
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Use protected when you intend to inherit from the class. Even then all of your members should be private, with protected functions to access them.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It's a very object oriented concept and you'd need some experience with the need for OOP before you got it. Basically, sometimes you can get problems if your whole program has access to the internal workings of a class. Public items can be accessed by anyone. They should generally only be certain functions that allow the appropriate accessibility to the outside. You may also want to have functions that return variables you don't want changed, so that the program in general can't gain direct access. Things like interfaces in Java give a better description - it's just a way for the class to communicate. Internally, you would have all the variables and other functions. private is for that class only. Protected allows classes derived from that class to access its data. The rest is up to you, and there are really no hard and fast rules. Just good design techniques you need to get used to.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    Originally posted by sean_mackrory
    It's a very object oriented concept and you'd need some experience with the need for OOP before you got it. Basically, sometimes you can get problems if your whole program has access to the internal workings of a class. Public items can be accessed by anyone. They should generally only be certain functions that allow the appropriate accessibility to the outside. You may also want to have functions that return variables you don't want changed, so that the program in general can't gain direct access. Things like interfaces in Java give a better description - it's just a way for the class to communicate. Internally, you would have all the variables and other functions. private is for that class only. Protected allows classes derived from that class to access its data. The rest is up to you, and there are really no hard and fast rules. Just good design techniques you need to get used to.
    Would you mind giving me a small example on "functions that return variables you don't want changed"?
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Usually, you make a variable in a class private when it shouldn't be changed directly without other internal data being updated along with it (ie: the object could fall into an unstable/invalid state, otherwise). Those interdependancies might not be apparent to the user, so by declaring as such, you are making it perfectly clear.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    okay, i think i understand now. But here's one more question: when returning the value of a variable in a class using a function, which one below is better?

    Code:
    int returnsomething(int member)
    {
             return member;
    }
    OR

    Code:
     int& returnsomething(int& member)
    {
             return member;
    }
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    If you are going to return a reference, make it a const reference - again, to enforce the concept that the data is private (I think that's what you were asking, anyway).

    ie:

    Code:
     struct myclass
    {
      const int & get_member() const
     {
      return member;
     } 
     private:
    int member;
    };

    Returning reference/pointers (remember that a reference is effectively the same as a pointer) is almost always more efficient than returning the data by value. That's because when you return by value, a copy of the object being returned created. That doesn't matter much if you're returning an integer, of course, but if the object you're returning is composed of 256 bytes - that will make a difference (pointers on the other hand, are only 4 bytes wide [on most machines, anyway] ).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    i believe what he ment was a function returning the value of a private data member.

    Code:
    class SomethingSimple (
    public:
    bool SomeBoolState()
    private:
    bool SomeBool;
    };
    
    bool SomeBoolState()
    {
      return SomethingSimple.SomeBool;
    }
    Its just to enforce them into thinking they can't touch that variable, because maybe some functions rely on it, and if user changes it, then sudenly they dont know what to expect because its onyl supose to be chanegd by those functions.

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    I got wat u guys were saying. Thx!
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Best "Menu" method?
    By SSJMetroid in forum Game Programming
    Replies: 11
    Last Post: 12-08-2005, 12:05 AM
  3. I need a code for Tic Tac Toe game ?
    By martyr in forum C++ Programming
    Replies: 11
    Last Post: 12-07-2003, 03:29 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. MFC Assertion Failure
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 08-01-2002, 09:58 AM