You are aware that those lines do absolutely nothing?
Ahhhh thanks a lot :], hey i was wondering if you could explain public and private members and functions to me please?im reading my book right now but, i dont really get it.
Let's take the car analogy again.
Your car has lots of components. An engine, an electrical system and who knows what else.
But the engineers of the car don't want you to go poking around in its internals, right? You hit the gas pedal and the car accelerates. You don't go around poking in the injectors or such.
Therefore, it would be convenient to allow you to use the gas pedal, but not access the injectors and whatnot.
We call the pedals the the interface. The functions that you use to use the functionality of the object. These functions are public.
The other stuff, like the injectors and things that shouldn't be accessed by you are private.
So to translate that into C++, basically you create an object from a class. Then you use its members. But we only want the users of the class to access certain members. So the members they can access are public, and those that they cannot are private.
Code:class MyClass { public: void foo() {} private: void bar() {} }; MyClass m; m.foo(); // OK: foo is public. m.bar(); // Compile error: bar is private.
"m" isn't trying to access bar(); you, the user, is trying to access it. And you are not "m". Therefore it yields a compile error.
OTOH, this works:
It works because the call to bar is inside foo which is inside the class. So basically, if MyClass is the car, you've basically told it to accelerate and it calls appropriate functions inside itself to make this happen.Code:class MyClass { private: void bar() {} public: void foo() { bar(); } }; MyClass m; m.foo(); // OK: foo is public.
But you can't tell it to pump more gasoline directly (as the user), because that function is private. OTOH, the car can do it since it's its internals.
so what im wondering is who CAN access private members then?
Oh wait so im reading in my book, private functions can only be called upon by other functions in the class?
And friend functions, and member functions of friend classes.Originally Posted by cuo741
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You'll find out if you are sociable enough with your book.Originally Posted by cuo741
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)