-2

C++: Difference between abstraction and encapsulation in c++

I have seen some answers on this topic but I want to know the difference by an example which relates to the theoretical concept of these topics.

Sagar Chauhan
  • 23
  • 1
  • 3
  • The two are not strictly defined terms in the language, so asking for an **exact** difference is not very meaningful. – BoBTFish May 25 '18 at 05:59
  • The crux of the matter is *abstraction* is a manner of modeling a physical or real-world object in code (e.g. `class bike`, etc..), *encapsulation* is a method of protecting data from manipulation by the user except through the accessor functions you provide to control the access (the class `private` data members provide a convenient form of encapsulation). Other than that, you will find a number of similar explanations. – David C. Rankin May 25 '18 at 06:03
  • @DavidC.Rankin Abstraction isn't limited to physical or real-world objects. Not even objects. – juanchopanza May 25 '18 at 06:10
  • 1
    I didn't intend any limitations by way of the example. I think of it more as the cat and the old lady and surgeon in [How abstraction and encapsulation differ?](https://stackoverflow.com/questions/16938667/how-abstraction-and-encapsulation-differ). It can be looked at from both a data or process standpoint. That's why I particularly agree with the no *exact* definition. It basically boils down to a collection of "What some guy said that gets reprinted" -- and there a lot of guys that have been reprinted on the topic. – David C. Rankin May 25 '18 at 06:13

1 Answers1

1

Abstraction is hiding the elements by making them private and/or proteceted in C++ in classes( basically structures too) so that they are secure and cannot be accessed from outside the class easily.

Encapsulation is combining of the data members and methods(functions) into a single entity, called Class. Encapsulation and abstraction are inter-related. Encapsulation implements abstraction.

Gopal Kaul
  • 27
  • 4