Solved

Data encapsulation ...!

Posted on 2000-05-14
8
295 Views
Last Modified: 2012-05-04
Can we write a program to get the facility of 'Data encapsulation' (one of the oop concept) in 'C' language ?
0
Comment
Question by:natarajfromiisc
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2809460
What data do you want to encapsulate?
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2809595
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.
0
 
LVL 10

Expert Comment

by:makerp
ID: 2809788
use C++ or java, and use the class constructs to achive proper encapsulation of data and methods.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Expert Comment

by:patoury
ID: 2810308
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.
Patoury.
0
 
LVL 4

Expert Comment

by:abancroft
ID: 2814583
You need to use an Abstract Data Type. This can be implemented in C using opaque pointers (void *).

e.g.
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;
} ADT, *PADT;

HADT CreateADT(void)
{
  PADT pRet = (PADT)malloc(sizeof(ADT));
  return (HADT)memset(pRet, NULL, sizeof(ADT));
}
void DestroyADT(HADT hADT)
{
  free(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.
0
 
LVL 3

Expert Comment

by:matth012098
ID: 2823918
Hi

  Data encapsulation can be achieved using typedef structures:

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


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).

Matth


0
 

Accepted Solution

by:
wraith0 earned 100 total points
ID: 2831229
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)
{
      pThis->x=0;
      pThis->GetXValue=&GetXValue;
      pThis->SetXValue=&SetXValue;
};
int GetXValue(struct Object *pThis)
{
      return pThis->x;
}
void SetXValue(struct Object *pThis, int xNew)
{
      pThis->x=xNew;
}
int main(int argc, char* argv[])
{
      struct Object myObject;
      myObject.Initialize=&Initialize;
      
      myObject.Initialize(&myObject);

      myObject.SetXValue(&myObject,4);

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

      return 0;
}


Hope this helps.
0
 

Author Comment

by:natarajfromiisc
ID: 2842093
Thanks alot, it is working fine
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I test for current date? 9 102
Global Keyboard Hooks Blocked 4 75
Unable to start eclipse ? 17 141
Using ANSI C how to Read a .csv file 10 79
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now