Sign in to follow this  

classes

This topic is 4685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'll give you an anlogy I gave the last person that asked this question. Think of your life as a class. Now, would you want everything 'public'? [lol] Same goes with programming, sometimes, you do not want everyone to know everything...Kind of see what I am saying?

- Drew

Share this post


Link to post
Share on other sites
Quote:
Original post by supercrazy7474
The only reason I can think of is for a header that is used by other people, to keep them from messing with the variables inside that class.


That's actually one of the most important reasons. If you have two classes, class A and class B, you don't want those classes to have to know anything about each other's internal representation. If A knows all about how B is implemented, and has access to all of B's private data, then as soon as a developer changes the implementation of B, A has to be changed as well. Now, imagine that instead of having just two classes, you have two hundred classes. If you change the implementation of just one, you have to change the implementation of 199 others to keep them up to date.

But if you declare everything that is part of the internal implementation of the class as private and give the class a public interface that will remain constant, then when you change a class, all other classes that use that class can remain the same.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I'm sure someone else could say this better than me, but I've just been thinking about this recently, -going over the concepts of c++ & oop. Personally I don't have all my classes private, but I think that was just for my own "ease" kind of thing, as a newbie programmer, but one of the principles of oop is that classes should (or can be), almost be like mini progerams within your program. A class should be designed to do one thing, or look after one principle, the reson it should be private is because it should be self contained, at the end of the day, almost the only thing you should need to know about it, is what it does and how to use it.

In my c++ book it says c++ & oop was designed to make working with large programs easier, thats one of the ways its done, instead of having to follow every line of code you just have to know what it does. Part of the reason it needs to be private is so that it can be a self contained object.

Share this post


Link to post
Share on other sites

This topic is 4685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this