Encapsulation in C++

This is a discussion on Encapsulation in C++ within the C++ Programming forums, part of the General Programming Boards category; So talking about the object-oriented paradigm of encapsulation, what are ways to implement it and ways to break it. From ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    31

    Encapsulation in C++

    So talking about the object-oriented paradigm of encapsulation, what are ways to implement it and ways to break it.

    From my experiences, structures and classes with private/protected members, and namespaces were ways to hide data and behaviours.

    Static objects/functions implemented in classes break encapsulation and data-hiding.

    Are there more ways to do either?

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    breaking encapsulation
    Code:
           class MyClass
           {
                private:
                   int a ;
                public: 
                    int & getA()
                    {
                        return a;
                    }
           };
           int main()
           {
               MyClass myClass;
               myClass.getA() = 10;
           }
    That is one way i think
    You ended that sentence with a preposition...Bastard!

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    btw...i don't think static object breaks encapsulation.. Just make it a const, and good luck trying to modify the mother...
    You ended that sentence with a preposition...Bastard!

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    31
    Now that i think about it, static doesnt necessarily break encapsulation. It can be a private static variable/function and other objects cant access it.
    I have never worked with a const static variable/function. Could you give me an example of how it would be used? (wouldn't #define potentially do the same thing?)

    Also, you do make a good point with using a class field to return the reference of a class member. I never recognized it as breaking encapsulation.
    Last edited by JohnLeeroy; 05-08-2011 at 04:59 PM.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,145
    Static objects/functions implemented in classes break encapsulation and data-hiding.
    False. State shared between instances of an object potentially complicates the implementation in the face of threading and such, but it doesn't necessarily have an effect on encapsulation.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-20-2011, 08:34 PM
  2. encapsulation and accessing large maps?
    By dustfinger in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2006, 06:51 PM
  3. encapsulation in c
    By swaugh in forum C Programming
    Replies: 2
    Last Post: 12-01-2004, 12:37 PM
  4. Integration or Encapsulation :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 06-02-2002, 09:14 AM
  5. Encapsulation window creation
    By xds4lx in forum Windows Programming
    Replies: 1
    Last Post: 11-09-2001, 02:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21