Results 1 to 5 of 5

Thread: Public vs Private (Classes)

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2000
    Posts
    47

    Public vs Private (Classes)

    I was wondering what is the difference between public and private variables in a class?
    Why should i make something private instead of making it public?
    Thanks

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Private variables cannot be accessed outside your class but public variable can be.
    Baaaaaaaaah

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2000
    Posts
    47
    Yea, thank you but I know that, maybe I didnt fully express wat my question was. Wy would i want to make the variables private so that they won't be accessed from outside the class, rather than make them public and let them be accessible throughout the whole program.
    And what kind of date would i want to keep private and what public?
    Thank You

  4. #4
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    It is hard to explain but here is what I found:



    Declaring methods or data private enables the compiler to find programming mistakes before they become bugs. Any programmer worth his consulting fees can find a way around privacy if he wants to. Stroustrup, the inventor of C++, said, "The C++ access control mechanisms provide protection against accident--not against fraud." (ARM, 1990.)
    Baaaaaaaaah

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    C++ is an object-oriented language, as you can tell since you're using classes. There are several principles in OO design, one of which is encapsulation. You are supposed to encapsulate a class's data within it so that it manages its own data.

    The reasonaing behind this is more or less to do with making each class as independant as possible. You provide public interfaces to your class that don't change (these are just public member functions). The interface defines the behaviour of the class and its interaction with things outside it. Then, inside the class, you implement the functionality you promised to provide in your interface, by dealing with privately held data and functions. This way, because you know that nothing outside the class can change what's inside it, you know that any bugs occurring that affect the data inside the class are due to code that is also within the class. By reducing the interdependencies between classes you make your code more managable and easier to debug.

    It's almost always a good idea to seperate the interface of something from its implementation, so that you can change the implementation if it's not satisfactory (if it's too slow for example) without affecting the way code outside your implementation calls it: all code using the implementation code has to go through the interface. The interface is like a contract to provide certain services.
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width