473,554 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Data hiding?

I premise I don't know C++ well but... I wondered what is this data hiding
thing... I mean, if I can look at the header (and i need it beacuse of the
class), then what's hidden?

Can someone give me an example of something hidden from the user?



Jul 22 '05 #1
11 4173
Lorenzo Villari wrote:
I premise I don't know C++ well but... I wondered what is this data hiding
thing... I mean, if I can look at the header (and i need it beacuse of the
class), then what's hidden?

Can someone give me an example of something hidden from the user?

Perhaps `hiding' is not the best term here.

The idea of private class members is that code that *uses* objects
instantiated from a class cannot access them (directly). This means
that the class' implementation can change without needing to change
client code.

HTH,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 22 '05 #2
> Perhaps `hiding' is not the best term here.

The idea of private class members is that code that *uses* objects
instantiated from a class cannot access them (directly). This means
that the class' implementation can change without needing to change
client code.

So this means I can see it but cannot use it? How useful is this?
Oh... maybe you meant those function can be used only by a particular class?
Is this like declaring static a function?

Jul 22 '05 #3
But wait a minute...

Lets try an example.

class myclass {
private:
int x;
mytype y;
public:
myclass(...);
};

Notice variable 'y', it's private and users of the class can't use it so we
say it is "hidden" or "encapsulat ed", but wait ... they will still have to
define it. What if the definition of 'mytype' requires a whole bunch of
special stuff located in a bunch of include files, the user is stuck to have
all those definitions in his name space. Then, what if there is a change in
one of those files, the user code won't have to be changed but it will have
to be recompiled.

To me this is C++ 's greatest flaw. I know there are all sorts of work
arounds but that makes things more complicated and messy. Interesting that
Lorenzo who "don't know C++ well " picked up on it right away.

Perry.
"Artie Gold" <ar*******@aust in.rr.com> wrote in message
news:3F******** ******@austin.r r.com...
Lorenzo Villari wrote:
I premise I don't know C++ well but... I wondered what is this data hiding thing... I mean, if I can look at the header (and i need it beacuse of the class), then what's hidden?

Can someone give me an example of something hidden from the user?

Perhaps `hiding' is not the best term here.

The idea of private class members is that code that *uses* objects
instantiated from a class cannot access them (directly). This means
that the class' implementation can change without needing to change
client code.

HTH,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 22 '05 #4
Lorenzo Villari wrote:
Perhaps `hiding' is not the best term here.

The idea of private class members is that code that *uses* objects
instantiate d from a class cannot access them (directly). This means
that the class' implementation can change without needing to change
client code.
So this means I can see it but cannot use it? How useful is this?


Now there's an interesting question! ;-)
Actually, a reason that private (as well as protected) members are
`visible' (as in `you can see them in source code') is so client
code can determine the size of an object instantiated from a class.
Oh... maybe you meant those function can be used only by a particular class?
Correct. Member functions of a class -- and friends -- can use
private members of that class; general client code cannot.
Is this like declaring static a function?

It's a similar concept.

A much better explanation of all this would be found in a good book
about C++. See http://www.accu.org for suggestions.

HTH,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 22 '05 #5
Lorenzo Villari wrote:
I premise I don't know C++ well but... I wondered what is this data hiding
thing... I mean, if I can look at the header (and i need it beacuse of the
class), then what's hidden?

Can someone give me an example of something hidden from the user?


The PIMPL idiom:

http://c2.com/cgi/wiki?PimplIdiom

The "fast pimpl" idiom:

http://www.gotw.ca/gotw/028.htm

and good old pure abstract classes:

This is where there is an "interface header" file (in this case
"library.h" ) - this file contains an "interface" description.
Specifics of the implementation are undefined. Also, there exists an
"implemnetation " file (in this case library.cpp) and this is where you
place class descriptions that "implement" the desired behaviour, and
finally there esists the "applicatio n" that uses the interface.

In this scenario (for want of a better demo) I define an array interface
"ArrayInterface " that has NO data members. The methods are virtual and
so the application has no description of the data inside the
implementation objects.

In this case I implement a really nasty hack of latent copying from the
reference WHICH IS WRONG because the assumptions are very excessive and
easily broken.

Anyhow - here is "data hiding" - this is used in real life even in C.

i.e.
// interface (library.h file)
//
class ArrayInterface
{
public:

virtual ~ArrayInterface () {}
virtual double & operator[]( int i ) = 0;
};

ArrayInterface * Factory( const char * type );

// implementation - library.cpp file

#include <vector>
#include <string>

//
// really sick - I know - it's an array that looks
// like doubles but is really a float - MEGGA nasty
// hackola - demonstration purposes only, don't try
// this at home.
//
class FloatArray : public ArrayInterface
{
std::vector<flo at> array;

double temp;
int temp_index;

friend ArrayInterface * Factory( const char * type );

FloatArray()
: temp_index( -1 )
{
}

void fixtemp()
{
if ( temp_index != -1 )
{
array[ temp_index ] = temp;
temp_index = -1;
}
}

double & operator[]( int i )
{
fixtemp();

if ( array.size() <= i )
{
array.resize( i + 1 );
}

temp = array[i];
temp_index = i;

return temp;
}
};

class DoubleArray : public ArrayInterface
{

friend ArrayInterface * Factory( const char * type );

std::vector<dou ble> array;

double & operator[]( int i )
{
if ( array.size() <= i )
{
array.resize( i + 1 );
}

return array[i];
}
};

ArrayInterface * Factory( const char * type )
{
if ( std::string( "float" ) == type )
{
return new FloatArray();
}
else
{
return new DoubleArray();
}
}

#include <iostream>

void FuncThatWorksWi thArray( ArrayInterface & an_array )
{
std::cout << "an_array[ 30 ] = " << an_array[ 30 ] << "\n";
std::cout << "an_array[ 20 ] = " << an_array[ 20 ] << "\n";

}

int main()
{

ArrayInterface & floater = * Factory( "float" );

ArrayInterface & doubler = * Factory( "double" );
floater[ 30 ] = 1.3;

std::cout << "floater[ 30 ] = " << floater[ 30 ] << "\n";

doubler[ 20 ] = floater[ 30 ];

std::cout << "doubler[ 20 ] = " << doubler[ 20 ] << "\n";

std::cout << "Doing floater\n";
FuncThatWorksWi thArray( floater );
std::cout << "Doing doubler\n";
FuncThatWorksWi thArray( doubler );
}

Jul 22 '05 #6
"Lorenzo Villari" <vl****@tiscali .it> wrote in message
news:AN******** *************@t wister2.libero. it...
I premise I don't know C++ well but... I wondered what is this data hiding
thing... I mean, if I can look at the header (and i need it beacuse of the
class), then what's hidden?

Can someone give me an example of something hidden from the user?


The point of data hiding is not to keep you from seeing it. If you're smart
you won't look anyway. The implementation should not concern you -- only the
interface. Any assumptions you make based on what you see in the
implementation just make your own code less maintainable.

If any data items in a class were not visible to the compiler, it could not
allocate memory correctly. So we do the best we can by declaring all data in
a class to be private to prevent the client from using the implementation
directly. In this sense it is (sort of) "hidden".

Google for PImpl (pointer to implementation) for a pattern related to this
issue.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #7
In article <s4************ **********@twis ter1.libero.it> ,
Lorenzo Villari <vl****@tiscali .it> wrote:

So this means I can see it but cannot use it? How useful is this?


You can see local variables inside an ordinary function, but you cannot
use them directly inside other functions. How useful is this?

:-)

--
Jon Bell <jt*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #8
The point of data hiding is not to keep you from seeing it. If you're
smart you won't look anyway. The implementation should not concern you -- only
Please explain "If you're smart you won't look anyway"...
interface. Any assumptions you make based on what you see in the
implementation just make your own code less maintainable. Why?
If any data items in a class were not visible to the compiler, it could
not allocate memory correctly. So we do the best we can by declaring all

Then is "this" pointer visible? and the compiler knows anyway you refer to a
variable of a class... without "this" being present

Anyway... thank you all for your answers... I'm will think about it...
and come with new questions!

Jul 22 '05 #9
Lorenzo Villari wrote:
The point of data hiding is not to keep you from seeing it. If you're
smart you won't look anyway. The implementation should not concern
you -- only
Please explain "If you're smart you won't look anyway"...


You're supposed to use the interface of a class and not care about how
it's implemented at all, so you shouldn't care about private members.
interface. Any assumptions you make based on what you see in the
implementation just make your own code less maintainable.

Why?


If the implementation changes, your code would have to change too. But
if a class "hides" its implementation details, and you only use the
public interface, that implementation can change without you needing to
change the code that uses it. A simple example:

class Foo
{
public:
Foo(double lengthInCM)
: centimeters_(le nghtInCM),
inch_(lengthInC M * cm_to_inch)

double centimeters() { return centimeters_; }
double inch() { return inch_; }

private:
static const double cm_to_inch = 0.394;

double centimeters_;
double inch_;
};

Notice how you cannot access the member variables centimeters_ and inch_
directly, but only through member functions. Now if the maintainer of
that class decides that it's better to calculate the lenghts
dynamically, he might change your class to:

class Foo
{
public:
Foo(double lengthInCM)
: centimeters_(le nghtInCM)

double centimeters() { return centimeters_; }
double inch() { return centimeters_ * cm_to_inch; }

private:
static const double cm_to_inch = 0.394;

double centimeters_;
};
Now there is no member variable inch_ anymore, but it doesn't matter,
since the interface didn't change, and the user code will work without
a change.
If any data items in a class were not visible to the compiler, it
could not allocate memory correctly. So we do the best we can by
declaring all

Then is "this" pointer visible?


I don't know what you mean. The "this" pointer is just a pointer to the
current object. It only exists in non-static member function of the
object's class.
and the compiler knows anyway you refer to a variable of a class...
without "this" being present


Foo f;
Foo* p = &f;

now p points to the object f. Within member functions of class Foo, you
can can get the same pointer under the name "this". There is no
difference between the "this" pointer and any other pointer to the
object.

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2378
by: coolwarrior | last post by:
Hi, 1_I want to know the difference between "data hiding" , "steganography" ,"watermarking" ,"capsulation" related to DSP. 2_There r plenty of informaion about data hiding for images on the web ,but I'm looking for a GOOD keyword for searching "signal/speech data hiding/steganography"...Do u have any suggestion about appropriate keywords or...
28
3386
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
8
4568
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
12
2073
by: Alex Hunsley | last post by:
There's no really specific questions in this post, but I'm looking for people's thought on the issues within... The two main versions I've encountered for data pseudo-hiding (encapsulation) in python are: method 1: _X - (single underscore) - just cosmetic, a convention to let someone
11
5630
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback to find out if I've done anything wrong. Also, I am working on this for an embedded environment, so if there are great inefficiencies with this...
2
7614
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means mentioning the class members(functions, typedefs, data) under the access control labels : public, protected, private. Encapsulation means providing...
162
10111
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language,...
0
7600
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7521
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7563
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5155
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3560
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2020
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1134
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
841
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.