answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

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

Wiki User

10y ago

You cannot actually circumvent encapsulation, you can only undermine it with poor design. Undermining encapsulation simply means that any encapsulation you intended cannot be enforced by the compiler because you haven't encapsulated your members correctly. Unlike the real world where encapsulation can often be easily circumvented (lock-picking, reverse-engineering, etc), class design can enforce strict encapsulation wherever it is required. But if you write sloppy code, you undermine any encapsulation you intended, and errant code can easily slip through the net.

Consider the following class:

struct bad

{

bad(): m_data(1) {}

void set_data(unsigned int data){ if(data) m_data=data; }

unsigned int m_data;

};

In the above example, the mutator, set_data(), acts as a gatekeeper, ensuring that m_data is always non-zero (the constructor ensures it is initialised to 1). But because this class is a struct, m_data is implicitly public, thus the encapsulation implied by set_data is completely undermined. Any code can therefore legitimately assign the value 0 directly to bad::m_data. It is not circumventing the encapsulation because, logically, there is no encapsulation to circumvent.

To rectify this problem, m_data must be declared private, as in the following:

struct good

{

good(): m_data(1) {}

void set_data(unsigned int data){ if(data) m_data=data; }

private:

unsigned int m_data;

};

Now it is impossible to circumvent set_data(). Encapsulation is now assured.

You will often hear it said that friend functions can undermine encapsulation, but this is, frankly, errant nonsense. Yes, friend functions do have unrestricted access to private members, and yes, they can circumvent even the most stringent encapsulation. But the same can be said of any member of the class. Friends are not members, but if a class allows an external function to gain private access, then that friend must follow the exact same rules of encapsulation as the members of the class itself -- otherwise it is distinctly un-friendly. And since you would never allow private access to code you have no control over, it's up to you to enforce the encapsulation in your own friend functions, just as it is up to you to ensure that your class members maintain encapsulation. Remember that encapsulation only exists from outside of the class interface, and that a friend is simply an extension of that same interface. External code (non friend functions) are forced to use the interface you provide, but they cannot circumvent the encapsulation that you yourself put in place, both within the class itself and within the friend function.

Protected access is always something to be wary of, as this can easily undermine encapsulation just as public access can. This is because you have no control over how other programmers might abuse the privilege when they derive new objects from your classes. Best practice dictates that private member variables should never be declared protected. Only private methods should be declared protected, and only when it is necessary for a derived class to be able to call those methods. This forces your derivatives to use the encapsulation provided by the public and protected interface. As the old saying goes, keep your powder dry; and keep your private member variables private.

Remember that no-one can circumvent encapsulation. If they can, then there simply is no encapsulation to begin with.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

Encapsulation is achieved through classes, where member data is declared private and made accessible outside of the class via the public and/or protected interfaces. Only the class itself and friends of the class have access to the private representation. Protected methods are the same as private methods but are also accessible to derived classes and their friends. Public methods are accessible to any code.

Friends are functions that require access to the private representation of a class but are not themselves members of the class. This could be because they are members of another class entirely or because they operate upon two or more classes but cannot be considered a member of any of them. However, because friend functions are declared by the class upon which they operate, they are as much a part of that class interface as any other member would be.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Where the c plus plus encapsulation done?
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.


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.


Why is c plus plus regarded as being a hybrid language?

C++ is regarded as hybrid because it is both procedural and objected oriented. A pure c program can be compiled and run on a c++ platform. At the same time, c++ also provides object oriented features like classes, polymorphism, encapsulation, abtraction, etc.


Can graphics in C programming done with compilers other than turbo c plus plus?

Of course.


Can abstraction encapsulation be achieved in C program if yes explain?

abstraction and encapsulation is one of the concepts of OOPs and C is not an OOP [Object Oriented Programming language] obviously abst & encap will not be supported by 'C' Abstraction & encapsulation is a concept of OOP [Object Oriented Programming] But, 'C' is not an OOP whereas it is a POP [Procedure oriented programming], so obviously 'C' does not support abstraction and encapsulation Answer Encapsulation is not inherently supported but it can be emulated in C. The use of static and extern keywords for functions are almost equivalent to your private and public keywords in java (encapsulation). Read up more on those keywords.. Structures become an object's attributes while functions accepting pointers the the said struct become its methods.

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 concepts of object oriented programming in c plus plus?

The concepts of OOP in C++ are the same as for OOP in any other programming language: abstraction, encapsulation, inheritance and polymorphism.


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 you can do in C that connot be done by C plus plus?

Nothing. In C++ you could write a C compiler. So, everything that can be done with C, can be also done in C++.


Why is c plus plus regarded as being a hybrid language?

C++ is regarded as hybrid because it is both procedural and objected oriented. A pure c program can be compiled and run on a c++ platform. At the same time, c++ also provides object oriented features like classes, polymorphism, encapsulation, abtraction, etc.


Can graphics in C programming done with compilers other than turbo c plus plus?

Of course.


Can abstraction encapsulation be achieved in C program if yes explain?

abstraction and encapsulation is one of the concepts of OOPs and C is not an OOP [Object Oriented Programming language] obviously abst & encap will not be supported by 'C' Abstraction & encapsulation is a concept of OOP [Object Oriented Programming] But, 'C' is not an OOP whereas it is a POP [Procedure oriented programming], so obviously 'C' does not support abstraction and encapsulation Answer Encapsulation is not inherently supported but it can be emulated in C. The use of static and extern keywords for functions are almost equivalent to your private and public keywords in java (encapsulation). Read up more on those keywords.. Structures become an object's attributes while functions accepting pointers the the said struct become its methods.


What is encapsulation in c?

Assume you were asking as in C#, not C. Because C is not an OO language, thus if there is encapsulation, it would be different.The encapsulation in any OO language is to hide the information of data and the implementation detail of a method.For example, you have a bank account as a private data member. Any one can ask you about the Balance of your bank account, e.g. GetDeposits(), an operation that is public. Wait, you now are richer, and you open 2 more bank accounts, the public still has the only accessing method GetDeposits(), to get your total deposits.The encapsulation in this abstraction are:1. The detail, the implementation of GetDeposits(), is not revealed to public.2. The data member, 1 account or 3 accounts, no one got impact, and no one knows. (in fact, no one knows how many bank accounts you have!)


Why c plus plus is not complete object oriented language?

because c++ supports all the basic concepts of oop :1.objects,2.classes,3.data abstraction and encapsulation,4.inheritance,5.polymorphism,6.dynamic binding,5.message passing.


How does c plus plus endeavor to represent the object oriented paradigm?

C++ endeavours to represent the object oriented programming paradigm through the use of classes. The four main pillars of OOP are encapsulation, inheritance, polymorphism and abstraction, which C++ primarily achieves through the use of classes, class hierarchies, virtual methods and templates.


Which is the most common encapsulation in use on BRI Interface?

Which is the most common encapsulation in use on BRI Interface ? A. SDLC B. ATN C. HDLC D. PPP


What is the name of the process of building a frame around network layer information?

Encapsulation. Actually, this is one type of encapsulation; encapsulation occurs at several layers.Encapsulation. Actually, this is one type of encapsulation; encapsulation occurs at several layers.Encapsulation. Actually, this is one type of encapsulation; encapsulation occurs at several layers.Encapsulation. Actually, this is one type of encapsulation; encapsulation occurs at several layers.