Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Data Abstraction

C++ Data Abstraction

Data abstraction means providing only key information to the outside world while hiding its underlying implementation details—that is, showing only 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: a television. You can turn it on and off, switch channels, adjust the volume, and add external components (such as 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, converts them, and finally displays them on screen.

Therefore, we can say a television separates its internal implementation from its external interface; you don't need to know its internal implementation principles, and can operate it directly through its external interface (such as the power button, remote control, volume controller).

Now, to get back on topic: in C++ programming, C++ classes providedata abstraction, which provides the possibility. They provide the outside world with a large number of public methods for manipulating object data, meaning the outside world does not actually know the class's internal implementation.

For example, your program can call the sort() sort function without needing to know the algorithm used to sort the data inside it. In fact, the underlying implementation of sorting may differ across library versions, but as long as the interface is unchanged, the function call works as usual.

In C++, we useclassto define our own abstract data types (ADT). You can use a class iostream 's cout object to output data to standard output, as shown below:

Example

#include

<

iostream

using

namespace

std

;

int

main

(

)

{

cout

<<

"

Hello C++

"

<<

endl

;

return

0

;

}

Here, you don't need to understand how cout cout displays text on the user's screen. You only need to know the public interface; the underlying implementation of cout can change freely.

Access Labels Enforce Abstraction

In C++, we use access labels to define a class's abstract interface. A class can contain zero or more access labels:

There is no limit to how often access labels may appear. Each access label specifies the access level for the member definitions that follow it. The specified access level remains in effect until the next access label or the closing brace of the class body is encountered.

Benefits of Data Abstraction

Data abstraction has two important advantages:

If data members are defined only in the private section of the class, the author who writes the class can change the data freely. If the implementation changes, you only need to inspect the class's code to see what impact the change causes. If the data is public, any function that directly accesses the old representation's data members may be affected.

Example of Data Abstraction

In a C++ program, any class with public and private members can serve as an example of data abstraction. See the example below:

Example

#include

<

iostream

using

namespace

std

;

class

Adder

{

public

:

// Constructor

Adder

(

int

i

=

0

)

{

total

=

i

;

}

// Public interface

void

addNum

(

int

number

)

{

total

+=

number

;

}

// Public interface

int

getTotal

(

)

{

return

total

;

}

;

private

:

// Data hidden from the outside

int

total

;

}

;

int

main

(

)

{

Adder

a

;

a

.

addNum

(

10

)

;

a

.

addNum

(

20

)

;

a

.

addNum

(

30

)

;

cout

<<

"

Total

"

<<

a

.

getTotal

(

)

<<

endl

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Total 60

The class above adds numbers and returns the sum. The public member addNum and getTotal is the public interface that users need to know in order to use the class. The private member total is something users don't need to know, yet it is necessary for the class to function correctly.

Design Strategy

Abstraction separates code into interface and implementation. So when designing components, you must keep the interface independent of the implementation, so that if the underlying implementation changes, the interface remains unchanged.

In this case, no matter which program uses the interface, the interface is unaffected; you only need to recompile with the latest implementation.