In this module, the basic concept of encapsulation
in C++'s implementation of object-oriented programming.
- Why encapsulation is central to object-oriented programming
- What classes and objects are
- How to write member functions as part of an abstract data type
- How to limit access to an ADT's member data and functions
- How a class differs from a struct
Encapsulation is a technique that extends the concept of abstraction from a strictly data phenomena to both data and functions.
If you think of the data abstraction
used in C which is the
, then it is easy to grasp the idea that a class encompasses what a structure did and then extends the concept to include the binding of functions into the single entity.
Abstraction is another object-oriented concept related to encapsulation and information hiding. Simply put, abstraction means dealing with the level of detail that is most appropriate to a given task.
It is the process of extracting a public interface from the inner details.
For example, the driver of a car needs to interact with
- gas pedal, and
The workings of the motor, drive train, and brake subsystem do not matter to the driver. A mechanic, on the other hand, works at a different level of abstraction, tuning the engine and bleeding the breaks
Abstraction is the process of encapsulating information with separate public and private interfaces. The private interfaces can be subject to information hiding.
The important lesson to take from all these definitions is to make our models understandable to other objects that have to interact with them. This means paying careful attention to small details. Ensure methods and properties have sensible names. When analyzing a system, objects typically represent nouns in the original problem, while methods are normally verbs. Attributes can often be picked up as adjectives, although if the attribute refers to another object that is part of the current object,
it will still likely be a noun. Name classes, attributes, and methods accordingly.
All C++ programs are composed of the following two fundamental elements:
- Program statements (code): This is the part of a program that performs actions and they are called functions.
- Program data: The data is the information of the program which affected by the program functions.
Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.
Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected and public members. By default, all items defined in a class are private. For example: