Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

encapsulation question.

Status
Not open for further replies.

Sidro

MIS
Sep 28, 2002
197
US
Hi,
Teachers stress to students the importance of datas and methods encapsulation in a class. Users should only access only datas and methods that you want them to through your client code.
My question is, suppose I intentionally made a class data members and methods public just for this question. How would one access the functions in the class when its compiled into an exe-file? Example, suppose I wrote a program below. I compiled it into an exe File and gave only the exe file to someone and ask them to find a way to
call the "Function1" function so that it can cout,"hello World!", how can one do that with just the exe-file. Is it possible to do that? Or did I Misunderstand the concept of encapsulation?

class Fun
{
public:
void Function1();
};
void Fun::Function()
{
cout<<&quot;Hello World!&quot;;

}
void main()
{
//Blank;
}
 
I don't think the main danger is after the code is compiled. What you have to worry about is the programmers using your class if there is no data encapsulation.

For example, if you have a time class with data members hour and minute, you know that hour should never go above 12 (unless you want to mess with military time), and minute should never go above 59. Let's say you've set your code up so that every time your minute reaches 61, it is set back to 0, and the hour is incremented. You also have it set up so that hour resets itself to 1 if it hits 13.

Now lets say that your data members are public, and there are several people in your development team that are relying on your time class. One of them sets his program up to set hour to 14, thinking you are using military time. Since your functions are only set to reset the time if hour is 13, then after several days, weeks, whatever of hour incrementing with no correction the time will be e.g. 8323:30 AM, which could have serious ramifications if the program, say, keeps track of important shipping times.

Now if you have the data members private, and only accessible with setTime functions, then you can have error checking on your data. If the hour is below twelve, you can set the time, or if not, you can display an error message, and set the time to a reasonable value.

It is because of these types of dangers that encapsulation is good programming practice.
 
Exactly. That is a very good example. As to your other question, you're a little confused. To access functions after compilation (well, typically ;-) ), you need a definition of the access points to each function/class, which are not provided with a compiled program. DLL's and static libs however fulfill this requirement, and hence their popularity. Here, the art of hiding your data is called &quot;data-hiding&quot;, appropriately enough, and any decent advanced text will shed some light on that for you. Hope that helps.
 
Data encapsulation, private/public members are terms of compillation stage. In .exe file there are no difference in accessing of private or public class members. Everything works through pointers to class objects and shifts of class members from starting point of the class object. Even in your C++ source you can access private members, using theyr shift pointers:

shift = (char *)&object.private_member - (char *)&object;

(int *)((char *)&object + shift) = 123;
 
Hi Mingis,
You have something that really interests me here but I dont quite get what you're saying. Can you please make it clearer to me or provide a commented example? So it is possible to acess a private member field using shift pointers?
 
Perhaps I have problems with my english terminology :)

Let say, address of class object in memory is 123456 and the distance of the member from beginning of that class (offset) is 10. So, the member of the class object is reached using address 123456+10=123466. In C++ it would look something like following:

offset = &object.member - &object;

pointer_to_member = &object + offset;

This is an approach the processor uses to accsess object members, independently, are they private or public. Data encapsulation is only high level language method to avoid errors by inappropriate use of data.
 
Mingis is correct conceptually. However, that code won't compile because, since the member is private, you can't access it to get its address. You can always try to guess the address of a private member by looking at the order its class's data is declared and its size.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top