answersLogoWhite

0

Encapsulation is the means by which an object contains all the information required to use the object. The object's behaviour is entirely contained within and ultimately determined by the object itself. In other words, an object is a self-contained entity. More importantly, it is not necessary to know how an object works in order to use it, provided you are familiar with the object's interface.

As a real-world example of encapsulation, you probably use a mobile phone every day but unless you are a telephone engineer you are unlikely to know the internal workings of your phone beyond inserting a SIM card and perhaps replacing the battery. For everything else you will generally hire the services of an engineer. The phone's interface alone provides all the features you expect so you can make and receive calls, send texts and e-mail, update your diary, take pictures and videos, browse the internet and so on. Each of these features is itself an example of encapsulation, all of which is encapsulated within the phone itself. Indeed, modern interfaces are so intuitive that you probably didn't need to consult the manual; you probably discovered new features simply by playing with the phone. You can do all that without knowing how your phone actually works, let alone how you are able to send and receive millions of bits of information around the world every day. These are implementation details that are either encapsulated within the phone's software or by your service provider. You do not need to know these details in order to use the phone.

In C++, encapsulation is one of the four cornerstones of object-oriented programming (OOP). The other three are inheritance, polymorphism and abstraction. We usually associate encapsulation with information hiding, however nothing is actually hidden by encapsulation; if you have access to the source code you can easily expose the implementation details. Encapsulation is more about limiting access to the implementation details rather than hiding it. Hiding information is really a function of the compiler; if objects of a class are exposed from a binary library then you probably won't have access to the source code. But you will have access to the class interface through the library's header file and that's all you should really need in order to use an object of that class; the implementation details are safely hidden from view.

Encapsulation also relates to an object's physical representation which usually has very limited access or indirect access. That is, when we extract information from an object, we usually expect a copy of that information, not the information itself. There are exceptions, of course, however information that is vital to the internal workings of an object must never be exposed outside of the class. These are known as the class invariants and virtually all non-trivial classes will have one or more invariants. A string, for instance, has a very important invariant in the form of its internal character buffer (a memory resource). If that buffer were exposed outside of the class, we could easily invalidate the object by taking ownership of that buffer and assigning one of our own. That must never be allowed to happen, thus the buffer is encapsulated by the string and all access to it strictly monitored by the string. It "owns" the resource and only the string should have direct access to it. Even though we have no direct access to the buffer, we can still modify it through the string's interface. The interface ensures that the object remains in a valid state, but allows us to work with the string in a predictable manner. We don't really care how or even where the string is physically stored, only that we can work with it.

Since objects are encapsulated, we can embed objects within other objects (a technique known as composition). Since objects know how to behave themselves and maintain their own validity, we can safely expose them outside of the class, but if we need to control how those objects are manipulated we can declare them private and provide our own more specialised mutators (setters) instead. These act as gatekeepers to ensure the embedded objects remain valid (with respect to our composition) and delegate to the embedded object only when a mutation is deemed valid.

User Avatar

Wiki User

10y ago

What else can I help you with?

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 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!)

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.


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 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.


What has the author Kirk C Benson written?

Kirk C. Benson has written: 'Modeling data encapsulation and a communication network for the National Training Center, Fort Irwin, CA' -- subject(s): SCENARIOS, ENCAPSULATION, ARMY TRAINING, COMMUNICATIONS NETWORKS, MODEL THEORY


What is b plus b plus b plus c plus c plus c plus c?

b+b+b+c+c+c+c =3b+4c