Data encapsulation in C
-
Hiii... During an interview I got a question like this A class looks like: Class A{ private: int x,y; public: getxy(int &,int &); setxy(int x,inty) } In above class functions outside the class can't access x and y, how you will do this in C language ( i.e., that is data encapsulation in C) Actually I couldn't answer this question. I think data encapsulation is possible in C, but if I say "yes" as answer then interviewer will ask how.So struggled to give an answer. Later I read somewhere data encapsulation is possible in C , and somewhere is specified that it is not possible, is only possible with oops languages.... Anyway anybody can give a good clarification....It will be useful for all Thanking you Krish
-
Hiii... During an interview I got a question like this A class looks like: Class A{ private: int x,y; public: getxy(int &,int &); setxy(int x,inty) } In above class functions outside the class can't access x and y, how you will do this in C language ( i.e., that is data encapsulation in C) Actually I couldn't answer this question. I think data encapsulation is possible in C, but if I say "yes" as answer then interviewer will ask how.So struggled to give an answer. Later I read somewhere data encapsulation is possible in C , and somewhere is specified that it is not possible, is only possible with oops languages.... Anyway anybody can give a good clarification....It will be useful for all Thanking you Krish
As I know, there are no "out-/inside" words in C :)
virtual void BeHappy() = 0;
-
Hiii... During an interview I got a question like this A class looks like: Class A{ private: int x,y; public: getxy(int &,int &); setxy(int x,inty) } In above class functions outside the class can't access x and y, how you will do this in C language ( i.e., that is data encapsulation in C) Actually I couldn't answer this question. I think data encapsulation is possible in C, but if I say "yes" as answer then interviewer will ask how.So struggled to give an answer. Later I read somewhere data encapsulation is possible in C , and somewhere is specified that it is not possible, is only possible with oops languages.... Anyway anybody can give a good clarification....It will be useful for all Thanking you Krish
Encapsulation is definitely my favourite feature of object oriented programming. In plain old C you have structs and unions to group variables together, though the access rights are limited. You can use "internal data structures" by not exposing them outside your module, for example by declaring them
static
in a C source file. With a little bit of organisation you can have "inner/outer" variables. So instead of the complier enforcing these access rights, the developer has to do it in C. It's really not much more than that, for example take a C++ class with private members and compile it into an object file or library. Then make a copy of the header file, change private to public... voila you can access all previously private members and the compiler/linker won't complain. Evil but possible. :) Hope it helps! /MWebchat in Europe :java: Now with 26% more Twitter
-
Hiii... During an interview I got a question like this A class looks like: Class A{ private: int x,y; public: getxy(int &,int &); setxy(int x,inty) } In above class functions outside the class can't access x and y, how you will do this in C language ( i.e., that is data encapsulation in C) Actually I couldn't answer this question. I think data encapsulation is possible in C, but if I say "yes" as answer then interviewer will ask how.So struggled to give an answer. Later I read somewhere data encapsulation is possible in C , and somewhere is specified that it is not possible, is only possible with oops languages.... Anyway anybody can give a good clarification....It will be useful for all Thanking you Krish
I found a article, with the help of example specifying how we can encapsulate the abstract types in C Just have a look with that [^]
Величие не Бога может быть недооценена.
-
Encapsulation is definitely my favourite feature of object oriented programming. In plain old C you have structs and unions to group variables together, though the access rights are limited. You can use "internal data structures" by not exposing them outside your module, for example by declaring them
static
in a C source file. With a little bit of organisation you can have "inner/outer" variables. So instead of the complier enforcing these access rights, the developer has to do it in C. It's really not much more than that, for example take a C++ class with private members and compile it into an object file or library. Then make a copy of the header file, change private to public... voila you can access all previously private members and the compiler/linker won't complain. Evil but possible. :) Hope it helps! /MWebchat in Europe :java: Now with 26% more Twitter
-
Hiii... During an interview I got a question like this A class looks like: Class A{ private: int x,y; public: getxy(int &,int &); setxy(int x,inty) } In above class functions outside the class can't access x and y, how you will do this in C language ( i.e., that is data encapsulation in C) Actually I couldn't answer this question. I think data encapsulation is possible in C, but if I say "yes" as answer then interviewer will ask how.So struggled to give an answer. Later I read somewhere data encapsulation is possible in C , and somewhere is specified that it is not possible, is only possible with oops languages.... Anyway anybody can give a good clarification....It will be useful for all Thanking you Krish
you can do it with a combination of Opaque pointers[^] and accessor functions.
-
you can do it with a combination of Opaque pointers[^] and accessor functions.
That! The idiom is sometimes called "Pimpl" (pointer to implementation).
-
Hiii... During an interview I got a question like this A class looks like: Class A{ private: int x,y; public: getxy(int &,int &); setxy(int x,inty) } In above class functions outside the class can't access x and y, how you will do this in C language ( i.e., that is data encapsulation in C) Actually I couldn't answer this question. I think data encapsulation is possible in C, but if I say "yes" as answer then interviewer will ask how.So struggled to give an answer. Later I read somewhere data encapsulation is possible in C , and somewhere is specified that it is not possible, is only possible with oops languages.... Anyway anybody can give a good clarification....It will be useful for all Thanking you Krish
I used to do this in C all time by leveraging how files and the keyword "static" are used. Like many before me, in some ways, I invented a
this
pointer without realizing it! And here's the thing: sometimes I even do something similar with C++, though it can usually be slightly more elegant. Most recently, I did something similar with a Win32 DLL that had a straight C interface (so it could be called from .NET.)