Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What is the difference between data hiding and abstraction in c++ ? How can we differentiate both of them with examples ?
Posted
Comments
Mehdi Gholam 24-Nov-11 12:46pm    
Homework?
Ashutosh_g 24-Nov-11 12:49pm    
no i am confused with these two terms....
BrainlessLabs.com 24-Nov-11 13:48pm    
At what point are you confused? Can you point out?
Ashutosh_g 24-Nov-11 13:52pm    
data hiding means hiding of data and abstraction is also hiding of data then where is the difference?

Absolutely incorrect question. What is the difference between apple and Apple?

Just a hint. Abstraction is philosophical category, very universal in programming. When you simply write a function, this is abstraction. If can be called from different places if code. If you add a parameter to a function, this is abstraction. Before, you have a hard-coded value, now you promoted it to a parameter, so a function can be called with different value. You abstracted out of concrete value by promoting it to a parameter. Almost everything a good developers writes is abstraction.

Data hiding is more specialized category. It is integral part of OOP, but not just OOP. For example, local variables are hidden from outer context.

—SA
 
Share this answer
 
Comments
Espen Harlinn 24-Nov-11 15:06pm    
Good points :)
Sergey Alexandrovich Kryukov 24-Nov-11 15:53pm    
Thank you, Espen.
--SA
Stefan_Lang 25-Nov-11 5:42am    
I don't quite agree with your first statement: abstraction can hide bahaviour as well as data, and data hiding can be achieved through other methods than abstraction, e. g. with delegates. They are not the same, even though one often coincides with the other.
Sergey Alexandrovich Kryukov 25-Nov-11 6:07am    
Which one do you count as my "first statement"? My first statement is that the question is incorrect, will you argue against that? I agree with all your statements, but none of my statements contradicts to any of yours.
--SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Nov-11 14:57pm    
Would be good short article if not the word "Difference" in the title. If two things are different it does not mean that a "difference" between them can be found.
Also, it's good to understand that both notions in questions are not just object-oriented, they worked well before -- please see my answer.
--SA
Above all answers are correct, I just wanted to a little more, Abstraction is hiding complexity and Encapsulation is hiding data, though you cannot separate the two very easily, but Encapsulation is more about security,like private members are hidden and public are shown to all. On other hand Abstraction means no need to show how complicated steps you have performed to do particular operation.

Regards
 
Share this answer
 
Read this short article by B. Stroustrup Why C++ isn't just an object-oriented programming language. What "abstraction" means in OOP and C++ in particular, Stroustrup knows better than others. There, in chapter 5 he described all fundamental C++ concepts and how they relate to abstraction. In short, encapsulation "is the enforcement of abstraction by mechanisms that prevent access to implementation details of an object or a group of objects except through a well-defined interface".
 
Share this answer
 
v3
Comments
enhzflep 24-Nov-11 19:34pm    
The answer I consider the best. My 5.
Sergey Alexandrovich Kryukov 25-Nov-11 6:12am    
"Stroustrup knows better than others" is not a rational argument, and only rational arguments should be taken into account. I, for example, totally disagree, because I found good amount of mistakes or misconceptions in Stroustrup, in particular, on the methodology.

An argument by authority is unacceptable (would be fine if you added a smile to it) and should not be even be tolerated. Even if you said "I know better" about yourself, it would be so-so... We all need to play using fair rules here, and the argument by authority is a strong violation of fair rules.

Please forgive me, but I voted 1 just because of this fragment.
--SA
Sergey Chepurin 25-Nov-11 7:16am    
You know, probably you are right. I don't mind if you have such a strict view on this. Actually, i meant that he knows better than others what "abstraction" is in C++. And frankly, i don't know how anyone can disagree with that.
In the end, we are here not for the "points" to collect, but to share the knowledge.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Nov-11 14:55pm    
The problem here is: both notions are not just object-oriented, worked well before OOP.
I provided a simple answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900