answersLogoWhite

0


Best Answer

A class is a description of an object, its attributes, and its methods. Its not a lot different than primitive types, such as int. int a; /* instantiate object of type int and call it a */ person b; /* instantiate object of type personand call it b */ The difference is that person is declared in a class specification, and is potentially much more complex than int. Still, you can think of both in the same context. The attributes (member variables) of a class contain the state of each instance of that class. You can (and usually do) declare those attributes as private, which means that only methods of the class can access them. This is data hiding. With data hiding, you can encapsulate the functionality of a class, exposing only the needed public interface. This way, if you need to change the way a class works internally, such as storing the person's name in unicode string instead of char string, that change can, if the interface is correct, be totally transparent to the user of the class.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Data hiding, or encapsulation, is achieved by limiting access to data members.

In the following example, we wish to store an integer within our class, but we also wish to assure ourselves that the integer is non-zero at all times. We do this by encapsulating the data member.

class object

{

public:

object(int data=1):m_data(1){SetData(data);}

object(const object& obj):m_data(obj.m_data){}

object& operator=(const object& obj){m_data=obj.m_data;}

int GetData() const {return(m_data);}

void SetData(int data){if( data ) m_data=data; }

private:

int m_data;

}

In the above example, the m_data data member is declared in the private section and is therefore completely hidden from outside of the class. That is, nothing outside of the class can modify its value directly, only the class itself and friends of the class have direct access to m_data.

The GetData() method is declared in the public section and allows consumers of the class to obtain the value of m_data. However, the value returned is simply a copy of m_data, not m_data itself (it is returned by value, not by reference). Thus consumers can modify the return value without affecting m_data. If the return value were instead returned by reference, then this would completely undermine the encapsulation. If a reference to m_data must be returned, we must make sure it is returned by constant reference to maintain the encapsulation.

The SetData() method is also declared in the public section and allows consumers to modify m_data. However, m_data is only modified if the value passed to the function is non-zero. If zero is passed, m_data is unaffected by SetData(). This adds an extra level of control over how m_data is manipulated from outside of the class. Set accessors such as this act as gatekeepers to the data, and must guarantee that the data remains in a valid state at all times. In this case, SetData() assures us that m_data is never zero. there is no other way to modify m_data from outside of the class, thus encapsulation is maintained.

Both constructors initialise m_data. The default constructor can accept an integer but will initialise m_data to 1 before calling the SetData() method. Again, this assures us that m_data is never zero. The copy constructor simply copies the m_data member from the object reference passed to it. Since all instances of a class are automatically friends of each other, we can be assured that the object reference parameter's m_data member is non-zero. Similarly with the assignment operator overload. Neither needs to call the SetData() member because the encapsulation assures us that the objects must be valid.

This is just a very simple example of encapsulation. Some encapsulations can be highly-complex, but ultimately they exist to assure us that the object remains in a valid state at all times, only exposing as much as is needed in order to make use of the object and nothing more. In this case, we can be assured that every call to GetData() will return a non-zero value, but we can easily modify the code to assure us that the value is not only non-zero but also within a given range, without affecting the consumers of the class in any way. So not only is the data encapsulated, but the implementation of the class is encapsulated as well.

Thus it is not necessary for consumers of the class to understand how a class does what it does, they need only know what it does. And so as long as we do not alter the interface, we could very easily replace m_data with an unsigned char, or even another class of object altogether. As far as the consumers are concerned, the object hasn't changed at all and it still works exactly as they expect it to work. This makes it incredibly easy to alter implementations without breaking any existing code, none of which would be possible without encapsulation.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A class in C++ is a type. It is similar to simple types such as int, float, char, etc., except that you define what the type consists of and what operations that type supports.

Lets suppose you implement a stack class. You could create an instance by saying stack mystack, or something like that, and you could push things on the stack by saying mystack.push(something), etc. You could also override the addition operator, for instance, and define push as mystack + something, although pop would be somewhat different, perhaps as the decrement operator, mystack--.

The concept of data hiding is simply that the implementation of the class exposes only the public interface, i.e. construct, destruct, push, and pop, while the internal aspects of how to do that, be they an array, linked list, queue, etc. only be the concern of the implementation.

So long as you do not change the public interface of a class, you can change the private implementation without needing to change anything in the user of the class.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

C++ does not support the concept of information hiding. You can limit access to data, but that is not the same as hiding data; the data is patently visible at all times, only the access is limited. The only way to physically hide information is to obfuscate that information within a binary file. But that has nothing whatsoever to do with object-oriented programming nor C++, that is just a by-product of compilation.

What you call information hiding, we call data abstraction. That is, an accessor may return an int, but the underlying member variable from which that data is realised may not be an int -- it could be a double, or a user-defined type. The point is that it does not matter how the data is stored and retrieved, only that you receive the data in the expected type. The internal details of the class are not hidden from you unless those details happen to be buried in a binary such as a library file.

By way of an example, consider the following class:

class foo

{

int m_data;

public:

int GetData()const { return( m_data ); }

};

This class has no abstraction because the return value from GetData is the same as the member variable. However, at a later stage you might decide to change the internal structure of the class and decide that a double would be better than an int. However, you don't want to break any code outside of the class so you use abstraction:

class foo

{

double m_data;

public:

int GetData()const { return((int) m_data ); }

double GetActualData()const { return( m_data ); }

};

Any existing code will still use the GetData method which still returns an int (using a static cast). So as far as the consumers of your class are concerned the class hasn't changed, even though the internal structure of your class has changed quite dramatically. This is what we mean by information hiding, but it's really called data abstraction; nothing is actually hidden unless you do not have access to the source code.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

There is no such concept as data-hiding in C++. The correct term is abstraction. Often you will hear people say that the private and protected members of a class are somehow hidden within the class, however the private keyword merely limits access to those members, it does not physically hide them. Data-hiding is actually a function of binary libraries and executables where data can be physically obfuscated by the machine code.

Abstraction is the means by which it is not necessary to know how an object stores or manipulates its data in order to be able to use the class. Usage of the class is determined by the exposed interface. The interface provides a degree of separation between the consumer of the class and the internal workings of the class. This simply means that class designers can alter the internal storage and implementation mechanisms of their classes without affecting the existing consumers of those classes, provided the abstract interface remains unaltered. This then limits the exposure of those changes to within the class itself, to friends of the class, and, in the case of protected members, to derivatives of the class. Limiting the exposure greatly reduces code maintenance costs as external code is unaffected by those changes.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

By declaring the data private or protected in the class. Private data is accessible to the class and friends of the class. Protected data is the same as private data but is also accessible to derived objects. Public data is fully exposed.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Data hiding, also known as encapsulation, is where the data is declared private to the class, making it inaccessible outside the methods of the class. Only the methods of the class can access the data. The external view is only what is presented in the public interface via the various accessors. By doing this consistently and correctly, you can change the underlying data representation without changing the public interface, and thus without changing the code that uses the class, You can encrypt the data, for instance, or provide access controls for security purposes.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago
You cannot use classes in C, only in C++. You hide data by making it private, or at least protected, in the class that uses it.


class object

{

public:

object( float data ):m_data(data);

private:

float m_data;

public:

double AsDouble() const { return((double) m_data ); }

int AsInt() const { return(( int ) m_data ); }

};


In the above example, object::m_data is completely hidden from outside of the class. The consumer only has access to copies of m_data via AsDouble() and AsInt(). The fact m_data is cast is immaterial; even without the cast, only a copy of the value will be returned (if we returned a reference to m_data, we'd undermine the encapsulation).


Data-hiding makes it possible to alter the implementation of the class without affecting the consumers of the class. For instance, the designer may choose to declare m_data as a double rather than a float. He is free to do so because m_data is not accessible outside of the class. Consumers only have access to the public methods and none of them would be affected by such a change.


This answer is:
User Avatar

User Avatar

Wiki User

12y ago

In c++, data hiding is enforced in classes using private data members !!!

it means that we cant directly access the private data members of a class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How data hiding is possible within a class in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Data hiding in oops?

Possible. More precisely, it is data-protection.


What is the difference between data hidding and data abstraction?

Abstraction: Abstraction refers to removal/reduction of irrelevant data or unnecessary data or confidential data from a Class. Data hiding: Data hiding is a feature provided by the abstraction for hiding the data from the class.


In ANSI C you can hide data by using private access specifier Justify?

One can always declare a datatype as static which will limit the scope to the file only. this way data hiding can be achived. For more clearance on the same please refer 'the C programming language'. Data hiding means only relevant data is visible to the user and all the background information is hidden from the user. In c++, the variables are named as data members and these can be hidden with the help of private access specifier. In procedural languages, variables(global variables) are free to flow from functions to functions and hence they were not secured. But in C++, only the class in which the data members are being declared can access them by using private specifier. As, the data members and also member functions of a class cannot be accessed outside the class if they have private access so, they get hidden from the user and hence the data hiding is achieved. Also in inheritance when we derive a class from the base class then the derived class cannot access the private members of the base class. In addition, if a class is derived from another class privately i.e. for example syntax : class B : private A , is used then all the public and protected members (not private) becomes private to class B and cannot be accessed outside the class B, even by using the object of class B. Hence, data hiding is achieved in C++ through private access specifier.


How data hiding is achieved in java?

Data hiding in the java is achieved through the use of encapsulation.


Why encapsulation does not implement information hiding in object orientation?

Encapsulation also implements data hiding in an object oriented programming design. By encapsulating various methods & data objects into a single class they can also be hidden from all the other classes. You can declare the variables and methods as private and that way you can hide the data from the other classes in the application.

Related questions

Data hiding in oops?

Possible. More precisely, it is data-protection.


What is Data Hiding in VBNET?

Data hiding is used method used in Object-Oriented programing to hide information within computer code


What are the benefits of data hiding?

Data hiding enhances security by restricting access to sensitive information, reduces complexity by encapsulating data within a class or module, and promotes modularity by allowing different components to interact without revealing internal details. It also helps in maintaining code integrity and avoiding unintended data manipulation.


What is the difference between data hidding and data abstraction?

Abstraction: Abstraction refers to removal/reduction of irrelevant data or unnecessary data or confidential data from a Class. Data hiding: Data hiding is a feature provided by the abstraction for hiding the data from the class.


What is data hidding in oop?

Data hiding in OOP refers to the practice of hiding the internal state of an object from the outside world. This is achieved by encapsulating the data within the object and only allowing access to it through specified methods, known as getters and setters. By hiding the data, we protect it from being modified unintentionally and improve the integrity of the object's internal structure.


What term in java is used to indicate that a class's internal state is hidden from its clients?

Encapsulation............Hiding the data from others


How can you hide a photo within a photo?

Yes - the technique is called steganography, which is the practice of hiding data within image files. However, the process is not practical for hiding multiple messages or images within images - the clue for anyone viewing the data is an unusually large image file. If privacy is a concern, encryption would be a better avenue to pursue.


In ANSI C you can hide data by using private access specifier Justify?

One can always declare a datatype as static which will limit the scope to the file only. this way data hiding can be achived. For more clearance on the same please refer 'the C programming language'. Data hiding means only relevant data is visible to the user and all the background information is hidden from the user. In c++, the variables are named as data members and these can be hidden with the help of private access specifier. In procedural languages, variables(global variables) are free to flow from functions to functions and hence they were not secured. But in C++, only the class in which the data members are being declared can access them by using private specifier. As, the data members and also member functions of a class cannot be accessed outside the class if they have private access so, they get hidden from the user and hence the data hiding is achieved. Also in inheritance when we derive a class from the base class then the derived class cannot access the private members of the base class. In addition, if a class is derived from another class privately i.e. for example syntax : class B : private A , is used then all the public and protected members (not private) becomes private to class B and cannot be accessed outside the class B, even by using the object of class B. Hence, data hiding is achieved in C++ through private access specifier.


Is it possible to move data within accumulator?

we use LDA ## where ## is an 8 bit hexadecimal data


How data hiding is achieved in java?

Data hiding in the java is achieved through the use of encapsulation.


What does it mean by encapsulation?

Encapsulation makes class fields private, preventing access to it from outside of the class. Essentially, this data is hidden from the rest of the program. It is possible to allow access to the fields via public methods.


Why encapsulation does not implement information hiding in object orientation?

Encapsulation also implements data hiding in an object oriented programming design. By encapsulating various methods & data objects into a single class they can also be hidden from all the other classes. You can declare the variables and methods as private and that way you can hide the data from the other classes in the application.