answersLogoWhite

0


Best Answer

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

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

Wiki User

10y ago

Abstraction is the means by which it is not necessary to know how an object works in order to use it. The abstraction is such that class designers can alter the implementations of their classes without affecting any code outside of the class, provided the abstract interface remains unaltered. This greatly reduces code maintenance by limiting the scope of alterations to the class itself, leaving the consumers of the class unaffected and therefore unaware of the changes.

Abstraction is often confused with data hiding or information hiding, but nothing is ever actually hidden in a class, not even private members. The private keyword merely limits access to the internal storage and implementation of the class, but does nothing whatsoever to actually hide them. Information hiding is actually a function of the compiler, where information is physically obfuscated by the procedural machine code, and has nothing to do with C++ itself (the source code).

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Encapsulation and abstraction often go hand in hand, but they are in fact different concepts. You will often hear people use the term data hiding in place of abstraction, however you cannot physically hide data in C++ (nor in any language for that matter). At best you can obfuscate data within a binary library or executable, but that's an entirely different concept to abstraction.

Encapsulation simply means to combine the data and the methods that operate upon that data to produce a single entity; an object. In other words, an object is a self-contained capsule that contains (encapsulates) all the information required to initialise and make use the object's data.

Abstraction is the means by which an object exposes an interface that adds a level of separation between the consumer and the object's data, such that the class designer may alter the underlying implementation of an object without affecting the consumers.

Used together, encapsulation and abstraction allows consumers to use an object, simply by knowing what the object does, without the need to know how it does it. This reflects many real-life objects, where it is not necessary to know how an object works in order to use it, you only need to know what it does.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Abstraction involves the facility to define objects that represent abstract "actors" that can perform work, report on and change their state, and "communicate" with other objects in the system. The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago
Abstraction

Abstraction is where a base class is designed non-specifically. It is intended to form a common, generic interface between related objects through the use of pure-virtual methods. When a class declares a pure-virtual method, it automatically becomes and abstract base class (ABC), otherwise known as an abstract data type (ADT). This means the base class cannot be instantiated by itself, you must derive a class from it, and provide a more specific implementation of the pure-virtual methods. Only derived classes that implement all the pure-virtual methods can be instantiated, otherwise they also become abstract themselves.

Since derived classes are guaranteed to be a "kind of" base class, there is no need to maintain runtime information nor dynamically cast between types (which is always an indication of a poor design). Since all derived classes have a common interface in the ADT, there is no need to know the specifics of the derived class. You simply maintain a pointer to the base class and call the generic methods it exposes. The more-specific derived classes will then "do the tight thing", all by themselves, calling their own implementations of those methods. Other than rendering a class abstract, pure-virtual methods differ from virtual methods in that the base class need not provide any implementation whatsoever -- the methods must be overridden by the derived class.

Polymorphism is related to abstraction, although it applies to non-abstract base classes as well. This permits you to instantiate objects via their base classes. For example:

class Base{}

class Derived : public Base {}

Base * p = new Derived();

Since Derived is a "kind of" Base, you can treat all its derivatives as a Base class. All virtual methods exposed by the Base class will automatically invoke any overrides in the Derived classes.

Encapsulation

Encapsulation describes the way in which an object is self-contained. All the data and methods required by the class are contained in the class itself, with all implementation details hidden from the end-user. A carefully-crafted interface exposes only as much as as is required to make use of the class, nothing more. this allows programmers to alter their implementations without fear of breaking existing code, so long as the interface remains the same.

Since the exposed interface determines how an object is used, the end-user is not concerned with how the object works, only in what it does. This is similar to driving a car; it is not necessary to understand how the internal combustion engine works in order to use the car. Unless you were mechanically minded, you'd pass the car to a mechanic for maintenance.

Even if you write your own classes, encapsulation is an immensely useful programming tool. Since every class encapsulates its own data and functionality, highly complex classes can be created by combining existing classes into new container classes, which can themselves be contained by other container classes, making ever more complex classes. These container classes need not concern themselves with the classes they contain; each class looks after itself, which is the definition of encapsulation. Without object-oriented programming, you'd be forced to use structures, which are really no different to a completely public class. A completely public class has no encapsulation as everything is exposed and there is no controlled interface (any code can manipulate the public variables in a class). And although you can discipline yourself to avoid abusing that exposure, data hiding and encapsulation will greatly simplify your programming, ensuring you never inadvertently do something you shouldn't be doing.

Ultimately, this ensures your program's data remains in a valid state at all times, without having to continually check. The class itself is entirely responsible for ensuring its own data remains valid at all times, because it encapsulates all the knowledge and functionality required to perform that validation, internally. Thus all validation can be delegated to the class, and the user need only concern themselves with the fact a pointer to a class instance (an actual object) is not NULL before calling the class methods. There is no need to validate that object references are not NULL as a NULL reference would automatically render the entire program invalid.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Encapsulation is not a C++ term, but an object-oriented term. Encapsulation allows us to combine data and the methods that operate upon that data, wrapping them up in a single entity known as a class. A class is merely a type, while an object is an instance of a class. As with real-world objects, every object must have a clearly-defined purpose which is fully encapsulated by the object itself. The object is entirely responsible for its own data, and any operation upon that data is delegated to the object itself. It is not necessary for consumers of the object to know how an object works, only in what they do, thus highly complex objects can be created by combining very simple objects, encapsulating their functionality but delegating that functionality to the objects themselves.

By way of example, suppose we have a class to represent a simple on/off switch. The switch can be on or off, so its data will be a variable that may be zero (off) or non-zero (on). It's methods are to toggle between these two states and to report its current state.

Now imagine we have another class that contains several switch objects, a switch bank class. This new class is not concerned with how those switches work, only in what they do. They can be toggled and they can report their current state, nothing more, nothing less. By the same token, the switches are not concerned with the fact they are part of a switch bank. The switches are also not concerned with what their state actually means; that functionality is delegated to the switch bank. It may be that the switches work independently of each other, or they work together. But the individual switches do not care what their purpose is any more than a real-life switch doesn't care whether it turns a light bulb or a car's air conditioning system on and off.

The upshot is that the switches encapsulate all the functionality of a switch, while the switch bank encapsulates all the functionality of a switch bank. Each has a clearly defined role to play, neither is concerned with how each of them work, and only the switch bank is concerned with what the switches actually represent (what they do, not how they do it). That, in a nutshell, is what we mean by encapsulation.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is encapsulation in c plus plus?
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 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