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?
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
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.
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"?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.
An Unofficial Cristiano Ronaldo Website : Ronaldo 7
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; }
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?
ORCode:int returnsomething(int member) { return member; }
Code:int& returnsomething(int& member) { return member; }
An Unofficial Cristiano Ronaldo Website : Ronaldo 7
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; }
i believe what he ment was a function returning the value of a private data member.
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.Code:class SomethingSimple ( public: bool SomeBoolState() private: bool SomeBool; }; bool SomeBoolState() { return SomethingSimple.SomeBool; }
I got wat u guys were saying. Thx!
An Unofficial Cristiano Ronaldo Website : Ronaldo 7