Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Data encapsulation in C

Data encapsulation in C

Scheduled Pinned Locked Moved C / C++ / MFC
oopquestioncareer
8 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krish_kumar
    wrote on last edited by
    #1

    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

    E M A C J 5 Replies Last reply
    0
    • K krish_kumar

      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

      E Offline
      E Offline
      Eugen Podsypalnikov
      wrote on last edited by
      #2

      As I know, there are no "out-/inside" words in C :)

      virtual void BeHappy() = 0;

      1 Reply Last reply
      0
      • K krish_kumar

        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

        M Offline
        M Offline
        Moak
        wrote on last edited by
        #3

        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! /M

        Webchat in Europe :java: Now with 26% more Twitter

        S 1 Reply Last reply
        0
        • K krish_kumar

          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

          A Offline
          A Offline
          Adam Roderick J
          wrote on last edited by
          #4

          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 [^]

          Величие не Бога может быть недооценена.

          1 Reply Last reply
          0
          • M Moak

            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! /M

            Webchat in Europe :java: Now with 26% more Twitter

            S Offline
            S Offline
            SachinBhave
            wrote on last edited by
            #5

            I guess this[^] is what you are talking about. Hope the link helps the OP.

            1 Reply Last reply
            0
            • K krish_kumar

              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

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              you can do it with a combination of Opaque pointers[^] and accessor functions.

              image processing toolkits | batch image processing

              N 1 Reply Last reply
              0
              • C Chris Losinger

                you can do it with a combination of Opaque pointers[^] and accessor functions.

                image processing toolkits | batch image processing

                N Offline
                N Offline
                Nemanja Trifunovic
                wrote on last edited by
                #7

                That! The idiom is sometimes called "Pimpl" (pointer to implementation).

                utf8-cpp

                1 Reply Last reply
                0
                • K krish_kumar

                  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

                  J Offline
                  J Offline
                  Joe Woodbury
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups