C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-07-2002, 09:50 PM   #1
Registered User
 
Join Date: Aug 2002
Posts: 4
Newbie question about the Private type in classes

Im really new, and you're all probably laughing by now .
But, what is the point of the private access type in classes? Is that just there so YOU don't accidentally modify that, and the compiler gives you a kick upside the head if you do? What am I overlooking? Are they really that useless?
Ryeguy457 is offline   Reply With Quote
Old 09-07-2002, 10:17 PM   #2
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 5,034
class Object {

private:

int safest;

protected:

int safe;

public:

int exposed;

};



class NewObject : public Object {

public:

void manipulate()
{
exposed = 12; //..fine...
safe = 2; //...fine. Derived classes can access "protected:"
safest = 6; //...cannot do! private to "Object" class...
}

};


int main()
{

Object ob;

ob.safest = 10;//...no way...
ob.safe = 12;//...nope
ob.exposed = 13; //...no problem

return 0;
};



Ok, so you knew all that, right? Just a refresher. The idea behind it all is "data-hiding". You can use it as a tool to remind yourself that you didn't want to be able to access a member of an objects class directly, (maybe there are butterfly effect implications) or similar reasons. In other realms of programming, this mechanism has helped software library authors control what goes in and out of a class instance, information-wise, providing a sort of controlled-component functionality. But there are many other possible reasons. The point is, unless it's necessary for you at the moment, don't bother. But when you DO need it, you'll be glad it there
Sebastiani is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question with accessing private data mikahell C++ Programming 3 01-18-2008 03:14 AM
Working with winAPI in classes (newbie question) Spyril Windows Programming 3 11-09-2007 03:21 PM
Type conversion question. ellis C++ Programming 5 10-17-2006 04:05 PM
How to fix misaligned assignment statements in the source code? biggyK C++ Programming 28 07-16-2006 11:35 PM
Using VC Toolkit 2003 Noobwaker Windows Programming 8 03-13-2006 07:33 AM


All times are GMT -6. The time now is 02:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22