YouTip LogoYouTip

Cpp Data Abstraction

# C++ Data Abstraction Data abstraction refers to providing only essential information to the outside world and hiding the background implementation details, that is, showing only the necessary information without presenting the details. Data abstraction is a programming (design) technique that relies on the separation of interface and implementation. Let's take a real-life example of a television. You can turn it on and off, change channels, adjust the volume, and add external components (like speakers, VCRs, DVD players), but you don't know its internal implementation details. That is, you don't know how it receives signals through cables, how it converts signals, and finally displays them on the screen. Therefore, we can say that the television separates its internal implementation from its external interface. You don't need to know its internal implementation principles; you can control the TV directly through its external interface (like the power button, remote control, volume controller). Now, let's get back to the point. In terms of C++ programming, C++ classes provide the possibility of **data abstraction**. They provide a large number of public methods for manipulating object data to the outside world. In other words, the outside world doesn't actually know the internal implementation of the class. For example, your program can call the **sort()** function without knowing the algorithm used to sort the data within the function. In fact, the underlying implementation of the function's sorting may vary depending on the version of the library. As long as the interface remains unchanged, the function call will work as usual. In C++, we use **classes** to define our own abstract data types (ADT). You can use the **cout** object of the **iostream** class to output data to the standard output, as shown below: ## Example #includeusing namespace std; int main(){cout<<"Hello C++"<<endl; return 0; } Here, you don't need to understand how **cout** displays text on the user's screen. You only need to know the public interface; the underlying implementation of cout can be freely changed. ## Access Labels Enforce Abstraction In C++, we use access labels to define the abstract interface of a class. A class can contain zero or more access labels: * Members defined under a public label are accessible by all parts of the program. The abstract view of a type's data is defined by its public members. * Members defined under a private label are not accessible to code that uses the class. The private section hides implementation details from the code that uses the type. There is no limit to the frequency of access labels. Each access label specifies the access level of the member definitions that follow it. The specified access level remains in effect until the next access label is encountered or the closing right brace of the class body is reached. ## Benefits of Data Abstraction Data abstraction has two important advantages: * The internal state of the class is protected from being corrupted by unintended user-level errors. * The class implementation may change over time to cope with evolving requirements.
← Input Output Operators OverloaBinary Operators Overloading β†’