My thinking is that some classes are designed to only be instantiated by explicit construction; however, I always write functions that copy or assign objects.
For example, if you don't create an operator = (), and a programmer tries to copy an object of your class, the compiler will try to do a bit-wise copy. This is not a good thing if you have a lot of memory allocation, or objects that need to be constructed within your class.
Ohh ok... Thanks... But I need to know why do we prevent client for accessing the objects of the class. Is it so that client doesn't modify the values of that particular class or it could be any other reason as well?
A class does not allow users to modify members by default because that's what a class does. I don't mean to be obtuse here, if you are writting direct copy and assignment operators (ones with no processing of the data) for EVERY member like kooth says (s)he is then you should be using a struct.
Most of the time you use private data members because the input for that variable needs to undergo some sort of bounds checking and\or conversion to prevent invalid data and undesired behaviour. Other times it's for securtiy reasons, for example Microsofts security token where you don't want just anybody or any process directly accessing it but at the same time they all need to use it somehow at one point or another.
Let me clarify what I said: I meant to say that I always create an operator = () and a copy constructor for my classes. I don't necessarily open up all of the internal variables with access/modifier functions. Computergeek01 is correct though: One should use a struct if the object is designed to be directly manipulated by the user.
He's also right about the private data members -- I thought coolswathi's question was about why we sometimes don't allow the entire objects to be copied or assigned. If I didn't answer that, I'll try again. :)
@ kooth: Ah, in that case I apologize for singling you out like that. So long as you remember "The Rule of Three" then you sound like you are doing it correctly.
@Computergeek01. Wow! The time here in California is 9:46am. I will look forward to seeing your referenced post about 2 hours from now.
Kidding! My point: the post times displayed depend on what time zone you are in. Your most recent post appears as May 9, 2011 at 9:27am here.
Yeah the thread wasn't long enough for me to href the message tag so I was grasping for another way to reference it. That's a good point though, I need to remember the world doesn't all live in the EST zone.