• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

Data encapsulation ...!

Can we write a program to get the facility of 'Data encapsulation' (one of the oop concept) in 'C' language ?
1 Solution
What data do you want to encapsulate?
Data encapsulation is a concept, as you rightly say, so it can be implemented to some degree in *any* language. For instance, whenever you use a STATIC variable in a function or .C file, you're effectively encapsulating the variable; only code with its local execution unit can access it.
use C++ or java, and use the class constructs to achive proper encapsulation of data and methods.
Identify and Prevent Potential Cyber-threats

Become the white hat who helps safeguard our interconnected world. Transform your career future by earning your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

If I understand well you want to write in object with a non-object language.
I ve already done that for a while when I didn t know about C++.
The encapsulation means that you want to control which object are calling functions and which properties are used for such or such objects.
So you can follow the following rules:

1)Create your objects as structures

2)Objects function in your case must control the objects by whitch they were called.
3) functions must always get in first parameter the caller object

Have fun.
You need to use an Abstract Data Type. This can be implemented in C using opaque pointers (void *).

In public header file (ADT.h)

typedef void * HADT; /* HADT is a handle to an ADT */

HADT CreateADT(void);
void DestroyADT(HADT hADT);

In private C file (ADT.c)

typedef struct {
  int m_n1;
  int m_n2;

HADT CreateADT(void)
  PADT pRet = (PADT)malloc(sizeof(ADT));
  return (HADT)memset(pRet, NULL, sizeof(ADT));
void DestroyADT(HADT hADT)

Now all access/manipulation of ADT structs has to be done through the opaque pointer (HADT). Obviously you'll need to add other functions so clients can manipulate HADT's.

  Data encapsulation can be achieved using typedef structures:

typedef struct {
    int a;
    int b;
    int c;

However in its simpist form cannot fully support public, private, protected or methods etc, etc, ect......

NB. You could support methods by way of function pointers using some kind of virtual table as with COM

I suppose you could implement this programmaticaly, but it would be much easier switching to C++ or if you arent too bothered anout speed then switch to Java (A most wonderful, pleasure to use language).


This is about as close as I can get:

void Initialize(struct Object *pThis);
int GetXValue(struct Object *pThis);
void SetXValue(struct Object *pThis, int xNew);

struct Object
      int x;
      void (__cdecl *Initialize)(void *);
      int (__cdecl *GetXValue)(void *);
      void (__cdecl *SetXValue)(void *, int);
void Initialize(struct Object *pThis)
int GetXValue(struct Object *pThis)
      return pThis->x;
void SetXValue(struct Object *pThis, int xNew)
int main(int argc, char* argv[])
      struct Object myObject;


      printf("%i\n", myObject.GetXValue(&myObject));

      return 0;

Hope this helps.
natarajfromiiscAuthor Commented:
Thanks alot, it is working fine
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

We Need Your Input!

WatchGuard is currently running a beta program for our new macOS Host Sensor for our Threat Detection and Response service. We're looking for more macOS users to help provide insight and feedback to help us make the product even better. Please sign up for our beta program today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now