Search code examples
cencapsulation

Encapsulation with Struct


I have a doubt regarding the concept of encapsulation.

Isn't C language also does the encapsulation by 'struct' data type?

the data variables and the functions present in the struct can only be called by the struct variable. and no other variable in the system. Isn't it data hiding?

I am trying to relate this concept to understand C++ encapsulation.


Solution

  • There really only exists "partial hiding" of structs in C. You can look at this post for a more detailed explanation:

    Is there any workaround for making a structure member somehow 'private' in C?

    However, encapsulation in C++ is relatively similar. You cannot completely hide all data members from the client code (even if they are in the private section). It is seemingly a "violation" of encapsulation, but this is the nature of the language.