What are abstractions?

I read Stroustrup's "C ++ Programming Language" and it mentions a lot of "abstractions":

Many of the most flexible, effective, and useful abstractions are associated with the parameterization of types (classes) and algorithms (functions) with other types and algorithms.

and

C ++ is a language for developing and using elegant and efficient abstractions.

Is this in any way related to abstract classes in C ++? Or using polymorphism, inheritance, or patterns?

Can anyone set an example?

+8
c ++ abstraction
source share
3 answers

abstraction (n) - the quality of work with ideas, not events

- source: Oxford English Dictionary

Stroustrup does not apply to abstract classes or other specific ideas in programming. Rather, he refers to the word abstraction itself.

Abstractions are mental helpers. They help us think in "theory", rather than direct application. Mathematics is the art of abstraction. Programming is the art of applied abstractions.

Abstractions help us form mental models, such as hierarchies, to help us think about things. Polymorphism is possible due to abstractions. Consider an example.

Example

1st level

I have Oleksiy Dobrodum. I relate to him as the Icy Good, I treat him as Icy Good, and all that he will ever be is Alexey Dobrud. All I do with it is for him. We are now at the first level of abstraction, or the most specific we will ever work with this Oleksiy Dobrodum.

I recently purchased Zach Latta, so now I have both Alex Dobrudum and Zach Latta.

More 1st level

I could apply to them both individually and to Oleksiy Dobrodum and as to Zach Latta, but this would quickly increase and turn out to be not flexible. Instead, we can simply unite Alexei Dobromum and Zach Latta and call them people. Now we have reached the level of abstraction 2. Instead of dealing with each person individually, we can refer to them as people. In doing this, we distracted the "implementation" or specific details of each person and began to focus on ideas, so we now think abstractly.

More 2nd level

Of course, we can abstract it further, but I hope you begin to understand the idea of ​​abstractions. The key conclusion from this is that abstraction hides details (or implementation). Hiding the details in the abstraction of our People, we allow ourselves to speak in general terms. We will briefly talk about how this applies when programming in the next section.

The use of abstractions

Now that we have touched on what abstraction is, apply it. Polymorphism is possible due to abstractions. Following the model of the previous example, let's say we have the following two classes:

class OleksiyDobrodum name = "Oleksiy Dobrodum" smarts = :mega-smart mood = :happy favorite_candy = :chocolate end class ZachLatta name = "Zach Latta" smarts = :so-so mood = :indifferent hair_color = :brown end 

If I want to interact with an instance of ZachLatta , I must refer to it specifically. The same goes for instances of OleksiyDobrodum .

 zach = new ZachLatta print zach.name oleksiy = new OleksiyDobrodum print oleksiy.favorite_candy 

If I create an abstract class called Human and inherit OleksiyDobrodum and ZachLatta from it, then I can abstract the implementation of both classes and simply refer to both instances of them as Human .

 class Human name smarts mood end class OleksiyDobrodum < Human name = "Oleksiy Dobrodum" smarts = :mega-smart mood = :happy favorite_candy = :chocolate end class ZachLatta < Human name = "Zach Latta" smarts = :so-so mood = :indifferent hair_color = :brown end 

Our class diagram now looks like this:

Class diagram for code above

I could pounce on implementation forever, but let me move on to our key take-outs.

Key takeaway

  • abstractions are ideas, not specific events.
  • to abstract from something, you need to move away from its implementation and think about big ideas.
  • abstractions can be used to efficiently manage code (and many others)
  • object-oriented programming is completely dependent on abstractions. see the above marker point.
+15
source share

In general programming, abstractions have an exact meaning and are called "concepts." The concept is defined as follows:

A concept is a set of requirements consisting of real expressions, related types, invariants, and guarantees of complexity. It is said that a type that satisfies the requirements models the concept. A concept can expand the requirements of another concept called refinement.

  • Valid expressions are C ++ expressions that must be successfully compiled for the objects participating in the expression as concept models.
  • Associated types are types associated with a modeling type, because they participate in one or more valid expressions. Typically, related types can be accessed either through typedefs nested in the class definition for the simulation type, or they can be accessed through the feature class.
  • Invariants are the temporal characteristics of objects that must always be true, that is, functions associated with objects must preserve these characteristics. Invariants often take the form of preconditions and post-conditions.
  • Guarantees of complexity are the maximum restrictions on how long one of the permissible expressions will run, or how many of its resources will be used in various resources. The concepts used in the C ++ Standard Library are documented on the SGI STL website.

Realization of the concept in real code can be performed in several ways. The classic OOP approach is to write an abstract base class containing valid expressions and related types. Concrete derived classes then provide invariants and guarantees of complexity. For patterns, valid expressions are more implicit and are checked only after instantiation. The concept of the implementation of the template is to type a duck : if it looks like a duck, tricks like a duck ....

The C ++ 0x development process has devoted a lot of effort to making concepts explicit in the code, but this is not included in the C ++ 11 standard . However, the Lite Concept version is likely to appear in the next C ++ 14 standard.

+2
source share

Yes, this is related to abstract classes in C ++ and is not limited to this context, he explained in general terms, saying that C ++ has full support for abstraction.

Example: In C ++, we can use class types or function calls in other types. For example, a function call can have a class type / function as a parameter, both a function and a class refer to an abstraction of the form (here the abstraction refers to hiding the definition of a function or class from the user)

+1
source share

All Articles