OO Encapsulation  «Prev  Next»
Lesson 4 Encapsulation
Objective Mechanism behind packaging data

Encapsulation and packaging Data and Behavior of an ADT in C++

Mechanism behind packaging data and ADT behavior C++ provides the encapsulation mechanism to implement ADTs. Encapsulation packages both the internal implementation details of the type and the externally available operations and functions that can act on variables of that type. The implementation details can be made inaccessible to code that uses the type. Code that uses the ADT is called the client code for the ADT. For example, a stack could be implemented as a fixed-length array, while the publicly available operations would include push and pop.Changing the internal implementation to a linked list should not affect how push and pop are used externally. Encapsulating the data and behavior of the stack keeps the implementation of the stack hidden from its clients, which means the client code using the stack will not need to be rewritten if the stack implementation changes.

Encapsulation and packaging of 1) data and 2) behavior of ADT in C++

In C++, encapsulation is the process of bundling both data (attributes) and behavior (methods or functions) into a single entity, typically a class, while controlling access to them to ensure that the internal implementation details are hidden from the outside world. This is fundamental to the concept of Abstract Data Types (ADTs) in C++, which provide a way to define data and behavior in a way that hides the underlying implementation details from the user. Let’s break down the encapsulation and packaging of data and behavior of an ADT in C++
Encapsulation and Packaging of Data
  • Data in C++ classes refers to the attributes or member variables that store the state of an object.
  • In an ADT, these data members are typically kept private or protected to enforce encapsulation. This means the internal state of an object cannot be accessed directly from outside the class, ensuring data integrity and preventing unwanted modifications.
  • The class or struct in C++ packages these data members, providing a structured way to define and store the attributes of an object.
    For example:
    class Tank {
    private:
       std::string periscope;
       std::string gunMantlet;
       // other private data members
    };
    
    In this case, the data (`periscope`, `gunMantlet`, etc.) is encapsulated within the class and cannot be directly accessed from outside.

Encapsulation and Packaging of Behavior
  • Behavior in C++ refers to the methods (or member functions) that operate on the encapsulated data. These functions define how the data can be accessed or manipulated.
  • In an ADT, behavior is often exposed via public member functions, which act as an interface to the outside world. The actual implementation details of these functions are hidden from the user, ensuring that the data is only manipulated in controlled ways.
  • These functions might be mutators (which modify data) or accessors (which retrieve data). This approach ensures that any interaction with the object is performed in a way that respects the internal structure and rules of the class.

For example:
class Tank {
private:
    std::string periscope;
    std::string gunMantlet;
    // other private data members

public:
    // Method to set the periscope
    void setPeriscope(const std::string& ps) {
        periscope = ps;
    }

    // Method to get the periscope
    std::string getPeriscope() const {
        return periscope;
    }

    // Other public methods representing behavior
};

In this case, the behavior (like setting or getting the value of the `periscope`) is encapsulated inside public methods. The data is not directly exposed, and the only way to interact with it is through these controlled methods.
Abstract Data Type (ADT) An Abstract Data Type (ADT) is a type whose internal structure (data) and operations (behavior) are hidden from the user, and only the interface (public methods) is exposed. The user of an ADT interacts with it purely through its defined methods, without knowing the specifics of its internal implementation.
For example, a Tank class that represents an ADT:
class Tank {
private:
   // Data members (hidden)
   std::string periscope;
   std::string gunMantlet;

public:
   // Public methods (interface)
   void setPeriscope(const std::string& ps) {
       periscope = ps;
   }
    
   std::string getPeriscope() const {
       return periscope;
   }

   void displayTankInfo() const {
       std::cout << "Periscope: " << periscope << "\n";
       std::cout << "Gun Mantlet: " << gunMantlet << "\n";
   }
};
In this ADT, the internal state (data members) is encapsulated and hidden, while the public methods allow the user to interact with the object without knowing the internal details. This is a key feature of encapsulation, as it separates the interface from the implementation.
Summary
  • Encapsulation of Data: In C++, data members (attributes) of a class are typically private, allowing controlled access through public methods. This protects the internal state of the object from unauthorized access or modifications.
  • Encapsulation of Behavior: The methods (behavior) that interact with the data are encapsulated within the class and define how the outside world interacts with the internal state. These methods act as the interface between the internal implementation and the user of the class.

Together, these principles help create a robust and flexible design, allowing the internal workings of a class (the ADT) to be changed without affecting external code that uses it.

A Class is a type you define

A class is a type that you define, as opposed to the types, such as int and char, that are already defined for you. A value for a class type is the set of values of the member variables. The interface of an ADT tells you how to use the ADT in your program. When you define an ADT as a C++ class, the interface consists of the public member functions of the class along with the comments that tell you how to use the public member functions. The interface of the ADT should be all you need to know in order to use the ADT in your program. The implementation of the ADT explains how this interface is realized as C++ code. The implementation of the ADT consists of the private members of the class and the definitions of both the public and private member functions.
Although you need the implementation in order to run a program that uses the ADT, you should not need to know anything about the implementation in order to write the rest of a program that uses the ADT; that is you should not need to know anything about the implementation in order to write the main part of the program and to write any nonmember functions used by the main part of the program.

SEMrush Software