Namespaces
Variants
Views
Actions

Understanding abstraction and its use in C++

From cppreference.com


Abstraction is the process by which the characteristics, properties and behaviors of a thing are identified for the purposes of making the implementation of the thing opaque.

[edit] Abstraction in the built-in numeric types of C++

In C++11, the simplest form of abstraction can be seen in the builtin types. For example, consider the various types of numbers that are available as builtin-types


- integers
- floating point


The basic abstraction is that of a Number. Any of the various number types can be used in expressions with any other of the various number types. Number is the basic abstraction that defines the interface or type. Numbers can be added, subtracted, multiplied, divided, modulo calculated, etc. Then from the basic abstraction (or type) of Number we get to major divisions, Integer numbers and Floating Point Numbers. The abstraction at this layer is used to differentiate numbers of values that are whole numbers in the set of {Z} and floating point numbers that are approximations of the numbers in the set of all real numbers, {R}.


It is at this point that we cross from the realm of abstractions into the realm of implementations where we see the different possibilities for the concrete, builtin number implementations (or classes) provided by C++.

- integer
signed
8, 16, 32, 64 and 128 bit signed integers
unsigned
8, 16, 32, 64 and 128 bit unsigned integers
- floating point
float
double
long double


From the perspective of the developer, we understand things like overflow, underflow and divide by zero that possible when dealing with numbers of differing implementations. But we also know that a float can be added to a short, and a short can be subtracted from a long. The basic properties of numbers, their signedness, and operational behaviors such as addition, subtraction, multiplication and division are all part of the C++ abstraction (type) for numbers. The implementation (class) of the numbers that used in a given expression define the possibilities of things such as overflow, underflow, and loss of precision.

[edit] Abstraction in the C++ standard library -- Containers

The C++ standard library provides a set of standard containers, that can be considered in terms of a number of basic abstractions.

- sequence
- list
- set
- map (or dictionary).

[edit] Abstraction in custom crafted code

Custom crafted code is code that is crafted by a developer to meet the needs of a given project.