473,708 Members | 2,345 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 4188
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
2387
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 websites? thanQ so much 4 ur care! coOlwarrior4ever
28
3415
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
8
4585
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
2085
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
5649
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 code, I'd like to know that also. I realize there is overhead with using an "accessor" function. ...
2
7637
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 the implementation of class member
162
10260
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, but why does it miss one of the basic principles of OOP? Will it ever be added to python?
0
8788
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8697
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9290
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9159
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7925
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5939
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2508
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.