answersLogoWhite

0


Best Answer

Data encapsulation is enforced by restricting access to the class members. Access can be specified on a per-member basis, defaulting to private access for a class and public access for a struct. Private members are accessible to class members and to friends of the class. Protected members are the same as private members but are also accessible to derived class members. Public members are fully-accessible. Data members are typically declared private while interfaces are typically declared public or protected.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

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

What is the term that describes the hiding of implementation details of objects from each other in a C plus plus class?

Encapsulation.


Where the c plus plus encapsulation done?

There are no statements as such. Encapsulation is a design concept. The basic principal of encapsulation is that an object should contain all the information necessary to use the object, nothing more and nothing less. In other words, an object is a self-contained entity.


What are the main features of OOP in c plus plus?

The main features of OOP are the same regardless of the language. They are: encapsulation; data hiding; inheritance; and polymorphism.


What is data encapsulation in c plus plus?

Data encapsulation refers to a means of limiting data access only to those functions that actually require direct access. Object-oriented programming languages such as C++ use access specifiers (public, private and protected) to determine the accessibility (or visibility) of an object's data or, more specifically, its representation.Public member data is accessible to any function (no encapsulation).Private member data is accessible only to the class member functions and to friends of the class (full encapsulation).Protected member data is the same as private member data and is also accessible to implementers of derived classes and to friends of those classes (partial encapsulation).For most classes, data members are declared private, thus fully encapsulating the data.


How data secured from out side world in c plus plus?

In C++, data is not secured from the outside world it is merely protected from accidental mutation. A determined programmer can easily circumvent the encapsulation of an object. Consider the following: class X { int data; // private by default }; Here, the data member is declared private (the default access for a class), however the class definition exposes the implementation details making it possible to define the exact same class with public access under a different name: struct Y { int data; }; Given that X and Y both have the same memory layout, it is trivial to undermine the encapsulation of an X by casting to a Y: void f (X& x) { x.data = 42; // error: x.data is private Y& y {(Y&) x}; // refer to x as if it were really a Y with pubic access y.data = 42; // ok }

Related questions

What is the term that describes the hiding of implementation details of objects from each other in a C plus plus class?

Encapsulation.


What are the main features of OOP in c plus plus?

The main features of OOP are the same regardless of the language. They are: encapsulation; data hiding; inheritance; and polymorphism.


Where the c plus plus encapsulation done?

There are no statements as such. Encapsulation is a design concept. The basic principal of encapsulation is that an object should contain all the information necessary to use the object, nothing more and nothing less. In other words, an object is a self-contained entity.


What is data encapsulation in c plus plus?

Data encapsulation refers to a means of limiting data access only to those functions that actually require direct access. Object-oriented programming languages such as C++ use access specifiers (public, private and protected) to determine the accessibility (or visibility) of an object's data or, more specifically, its representation.Public member data is accessible to any function (no encapsulation).Private member data is accessible only to the class member functions and to friends of the class (full encapsulation).Protected member data is the same as private member data and is also accessible to implementers of derived classes and to friends of those classes (partial encapsulation).For most classes, data members are declared private, thus fully encapsulating the data.


How data secured from out side world in c plus plus?

In C++, data is not secured from the outside world it is merely protected from accidental mutation. A determined programmer can easily circumvent the encapsulation of an object. Consider the following: class X { int data; // private by default }; Here, the data member is declared private (the default access for a class), however the class definition exposes the implementation details making it possible to define the exact same class with public access under a different name: struct Y { int data; }; Given that X and Y both have the same memory layout, it is trivial to undermine the encapsulation of an X by casting to a Y: void f (X& x) { x.data = 42; // error: x.data is private Y& y {(Y&) x}; // refer to x as if it were really a Y with pubic access y.data = 42; // ok }


What is meant by inheritance in c plus plus language?

Inheritance in C++ and in other Object Oriented languages is the creation of a class that incorporates a different class. The child (or derived) class "inherits" all of the elements (attributes and methods) of the parent (or base) class. Depending on the design of the base class, the derived class can use methods and attributes of the base class as if they were its own. Typically, however, attributes of the base class are private to the base class and inaccessible to the derived class so as to maintain class hierarchy and data encapsulation.


How can you hide the data in a class in C plus plus?

If this is a homework assignment, please consider trying to answer it yourself first, otherwise the value of the reinforcement of the lesson offered by the assignment will be lost on you.To hide the data in a class in C++, simply declare the data private, and then manipulate the data using the public interface of the class.


What is friend class in c plus plus?

A friend class is any class that is granted access to the private members of another class. The following minimal example demonstrates a simplistic friend class. #include<iostream> class A { friend class B; public: A(int data=0):m_data(data){} private: int m_data; }; class B{ public: B(const A& a):m_data(a.m_data){} int getdata()const{return(m_data);} private: int m_data; }; int main() { A a(50); B b(a); std::cout<<b.getdata()<<std::endl; // prints 50 return(0); } In the above example, A's encapsulation is such that A:m_data can be initialised through A's constructor but is otherwise inaccessible outside of the class. The assumption is that the A::m_data is intended for internal use only. However, B's encapsulation is such that it can be constructed from an instance of A, but since there is no public accessor to A::m_data, B requires friend access to A, and only A can grant that access, hence class B is declared a friend of A. Note that friends can be declared any where in a class declaration since private, protected and public access do not apply. Note also that there is no need for a forward declaration of B before declaring A because B is dependant upon A, not A upon B. It should also be noted that although B could potentially undermine the encapsulation of A (a common complaint where friends are concerned), the assumption is that since you are in control of both A and B, then it is your responsibility to ensure that B obeys the same encapsulation rules you set out within A. That is, if A provided a private A::getdata() accessor and a corresponding A::setdata() mutator for its own internal use, then B should make use of that same accessor and mutator rather than access A::m_data directly. This ensures that should you alter the implementation of A::m_data, you will not affect the inner workings of B, thus enforcing A's encapsulation rather than undermining it, and therefore minimising maintenance. Friend classes are often used wherever two classes work closely together. For instance, a parent and child class often work in tandem, thus it can often make sense to for the child class to declare the parent class a friend. However, there are often better ways than declaring an entire class to be a friend of another class. Friend functions (which can include specific class methods) greatly reduce the chances of undermining encapsulation by limiting the exposure of your private class members. Thus instead of declaring the entire parent class to be a friend of the child class, you could simply declare specific functions to be friends of both classes, thus those functions provide the "glue" that binds the two classes together. Undermining encapsulation is always a danger with friends, thus you should never grant friendship unnecessarily. After all, the onus is upon you to enforce your own encapsulation rules -- the compiler cannot help you any more than it can help you enforce those rules within the class itself. However, if the public interface is sufficient for your needs, then you do not need friends. You should only use friends when the public interface is insufficient and where altering the interface to suit would actually do far more to undermine the encapsulation than a friend would.


What is message passing in c plus plus?

-define class with necessary data member & member function. -create object of that class. -communication.


What is the order of initialization of data in C plus plus?

The general order of initialization is:Base class objects (if present)Member data objectsConstructor function code


What is the difference between class data type and basic type in c plus plus .How can someone know which one is class type and which one is basic type.?

Basic types (primitive data types) have no methods associated with them.


When do you use the protected access specifier on a class member in c plus plus?

You use the protected access specifier to allow a derived class implementer access to what would otherwise be a private member method of your class. Private member methods are intended for internal use only, however some may be optimised versions of public member functions. Public member functions, particularly accessors (getters) and mutators (setters), often contain additional runtime checks that are usually redundant to the internal implementation. While we may not wish to allow "ordinary" users access to these optimised functions, derived class implementers are not ordinary users. Like you (the class designer) they want to create an efficient implementation and would therefore benefit from having access to your private implementations, or at least some of them. We achieve this by declaring those methods protected. Note that although you can also allow class implementers protected access to your otherwise private member data, this is not recommended as class implementers would then be able to undermine your data encapsulation. Class representations must remain private to maintain encapsulation. Similarly, any private methods that have potential for undermining encapsulation should likewise remain private.