Page 1 of 2 12 LastLast
Results 1 to 16 of 26

Thread: Encapsulation in C++

  1. #1
    Xcelsion... In Disguise. Xaneden's Avatar
    Join Date
    Nov 2004
    Location
    United Kingdom
    Posts
    1,699
    Thanks
    0
    Thanked
    0 times in 0 posts

    Encapsulation in C++

    Hey Guys,
    I'm currently in the process of learning C++, and I'm attempting to get my head around encapsulation. I understand that it means combining the code and data in a self contained 'box' which can be made private or public, and can therefore be used by other parts of the program. However, the books I have and all the sites I have checked seem to give a rather haphazard explanation of what an 'object' and 'class' is in relation to this encapsulation.

    I'd appreciate it if someone could quickly explain the basic theory of what they are etc. Small words and analogies would be appreciated.
    New Sig on the Way...

  2. #2
    Hexus.net Troll Dougal's Avatar
    Join Date
    Jun 2005
    Location
    In your eyeball.
    Posts
    2,750
    Thanks
    0
    Thanked
    0 times in 0 posts
    Simplest way that we were taught is encapsulation hides the internals from exterior access.

    So in a class"private" members can only be accessed by functions in the class they are declared in. So if an int 'a' is declared as private you cannot refer to it as class_name.a, but you can have a public funtion "get_a" which returns 'a' class_name.get_a;

    Public can be read and writen from outside the class, so public int b can be accessed via class_name.b and can be written to, less secure then private.

    Protected is an extended private that can be accessed by inherited classes but not externally.
    Quote Originally Posted by Errr...me
    I MSN offline people
    6014 3DMk 05

  3. #3
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Encapsulation isn't C++ specific, the thing to remember is the same principles applies to many languages.

    It's simply hiding the data by not allowing it directly accessible via the class. As Dougal said, you write a function in order to access that particular variable.

    99% of the time your variables will be private anyway and encapsulation is used pretty much all the time. It's really a simple concept, the Java docs have some good illustrated tutorials on encapsulation

    For example;



    This image shows on the outside the methods and on the inside the variables. The variables are encapsulated in that class and you'd need a method on the outside in order to access the variable on the inside (provided that it is explicitly declared as private).

  4. #4
    Senior Member
    Join Date
    Jan 2005
    Location
    Manchester
    Posts
    2,899
    Thanks
    67
    Thanked
    180 times in 135 posts
    • Butcher's system
      • Motherboard:
      • MSI Z97 Gaming 3
      • CPU:
      • i7-4790K
      • Memory:
      • 8 GB Corsair 1866 MHz
      • Storage:
      • 120GB SSD, 240GB SSD, 2TB HDD
      • Graphics card(s):
      • MSI GTX 970
      • PSU:
      • Antec 650W
      • Case:
      • Big Black Cube!
      • Operating System:
      • Windows 7
    For the noddy object and class descriptions:

    A class is like a blueprint for something. It tells the compiler how to make an object.

    An object is what it says. It's a piece of data that represents an object of some sort.


    Quote Originally Posted by KeZZerR
    The variables are encapsulated in that class and you'd need a method on the outside in order to access the variable on the inside (provided that it is explicitly declared as private).
    Private is the default access specification in C++; unless you explicitly declare members as protected or public they are private.

  5. #5
    Registered+
    Join Date
    Aug 2005
    Location
    London
    Posts
    44
    Thanks
    0
    Thanked
    0 times in 0 posts
    Quote Originally Posted by Butcher

    Private is the default access specification in C++; unless you explicitly declare members as protected or public they are private.

    Unless your implementing a struct, rather than a class, in which case the members are public by default

  6. #6
    Senior Member
    Join Date
    Jan 2005
    Location
    Manchester
    Posts
    2,899
    Thanks
    67
    Thanked
    180 times in 135 posts
    • Butcher's system
      • Motherboard:
      • MSI Z97 Gaming 3
      • CPU:
      • i7-4790K
      • Memory:
      • 8 GB Corsair 1866 MHz
      • Storage:
      • 120GB SSD, 240GB SSD, 2TB HDD
      • Graphics card(s):
      • MSI GTX 970
      • PSU:
      • Antec 650W
      • Case:
      • Big Black Cube!
      • Operating System:
      • Windows 7
    Yes, but generally when discussing encapsulation and the like people use the class keyword, not that a class and a struct are any different in C++, it's just the way people are usually tought.

  7. #7
    Hexus.net Troll Dougal's Avatar
    Join Date
    Jun 2005
    Location
    In your eyeball.
    Posts
    2,750
    Thanks
    0
    Thanked
    0 times in 0 posts
    I'd always been taught structs were nowhere near as flexible as classes for polymorphism.

    But we were taught both (structs first then classes), and I choose not to use structs as much as classes.
    Quote Originally Posted by Errr...me
    I MSN offline people
    6014 3DMk 05

  8. #8
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    I'm a Java monkey so undefined access modifier for a variable would define that particular variable as package access. I've got no experience with structs at all, i don't believe that concept exists in Java. Although, Generic types are handy!

  9. #9
    Registered+
    Join Date
    Aug 2005
    Posts
    19
    Thanks
    0
    Thanked
    0 times in 0 posts
    Low coupling, high cohesion! Yeah, basically you want to design classes/modules in a way that will support reuse and improve the maintainability of the software system as a whole i.e. you should be able to replace a module without having to change other modules as long as the interfaces to the module are implemented correctly.

  10. #10
    Senior Member
    Join Date
    Jan 2005
    Location
    Manchester
    Posts
    2,899
    Thanks
    67
    Thanked
    180 times in 135 posts
    • Butcher's system
      • Motherboard:
      • MSI Z97 Gaming 3
      • CPU:
      • i7-4790K
      • Memory:
      • 8 GB Corsair 1866 MHz
      • Storage:
      • 120GB SSD, 240GB SSD, 2TB HDD
      • Graphics card(s):
      • MSI GTX 970
      • PSU:
      • Antec 650W
      • Case:
      • Big Black Cube!
      • Operating System:
      • Windows 7
    Quote Originally Posted by Dougal
    I'd always been taught structs were nowhere near as flexible as classes for polymorphism.

    But we were taught both (structs first then classes), and I choose not to use structs as much as classes.
    Structs and classes are one and the same in C++. They both define a class. The only difference is that a struct has public access by default and a class has private access by default. Other than that they are identical.

  11. #11
    Xcelsion... In Disguise. Xaneden's Avatar
    Join Date
    Nov 2004
    Location
    United Kingdom
    Posts
    1,699
    Thanks
    0
    Thanked
    0 times in 0 posts
    Thanks for your help guys, but what I am asking is 'what is a class' and 'what is an object'. I am currently learning the 'theory' side of things, so the int etc things you have been talking about are going over my head

    Maybe it'd be best if I explain what I understand already and you guys could fill in the gaps (or rewrite if I'm wrong ).

    Encapsulation is where the code and data are kept in a 'black box', and therefore becomes a seperated functioning mechanism from the rest of the program. You can edit whats in this 'box', as long as it does the same thing and therefore can be used in the same way with other parts of the program. So, the code beneath may have changed but the facade stays the same, so there would be no difference to what the program does (the output would be the same).

    So in relation to this, what is a 'class' and what is an 'object'. Thanks for any help you guys can give
    New Sig on the Way...

  12. #12
    Registered+
    Join Date
    Aug 2005
    Location
    London
    Posts
    44
    Thanks
    0
    Thanked
    0 times in 0 posts
    Put simply, a class is like a set of plans that define what an object will look like. An object is an instance of a class. What you say above about encapsulation is pretty much correct. I've always thought of encapsulation as being analogous to driving a car. The steering wheel, pedals, hand brake etc... (the "public" members) are all familiar to the user, it's just that the engine, brakes, gearbox, suspension etc... (the "private" members) can vary significantly from car to car. However, because you know what to do with a steering wheel etc... the fact that the fundamentals of the car may be different from one car to the next doesn't matter. The interface is the same so you can still drive it.
    Last edited by GizmoStu; 16-08-2005 at 09:29 AM.

  13. #13
    Hexus.net Troll Dougal's Avatar
    Join Date
    Jun 2005
    Location
    In your eyeball.
    Posts
    2,750
    Thanks
    0
    Thanked
    0 times in 0 posts
    Quote Originally Posted by Butcher
    Structs and classes are one and the same in C++. They both define a class. The only difference is that a struct has public access by default and a class has private access by default. Other than that they are identical.
    And you contradicted yourself in that one sentance.

    But no flaming and back to the topic.

    The 'class' is like the blueprint for an instance of that class.

    You define a class with internal variables and functions, the functions are normally public and the variables private (this depends on the programmers choice) but you can have multiple instances of a class.

    An object and encapsulation in the way you're looking for is when you compile a piece of the code so the internal functioning cannot be seen, compiling in this way normally leaves you with a ".o" file which is then linked.

    But the header can still be viewed by the other programmer.

    For instance:

    I create a class which has a few functions. I then create an object file from this and pass it onto someone else to program with and use, along with the header.
    Now the person I pass this onto does not NEED to know how the functions actually work (and I may not want them to easily know how they work) but they need to know the function prototypes and comments in my header so they can use these functions correctly.

    So think of it like a PC.

    I build a computer and program it how to do stuff, all you need to know is how to use it.

    The computer is the object (black box) and the manual is the header and tells you how to use it.
    Quote Originally Posted by Errr...me
    I MSN offline people
    6014 3DMk 05

  14. #14
    Senior Member
    Join Date
    Jan 2005
    Location
    Manchester
    Posts
    2,899
    Thanks
    67
    Thanked
    180 times in 135 posts
    • Butcher's system
      • Motherboard:
      • MSI Z97 Gaming 3
      • CPU:
      • i7-4790K
      • Memory:
      • 8 GB Corsair 1866 MHz
      • Storage:
      • 120GB SSD, 240GB SSD, 2TB HDD
      • Graphics card(s):
      • MSI GTX 970
      • PSU:
      • Antec 650W
      • Case:
      • Big Black Cube!
      • Operating System:
      • Windows 7
    Quote Originally Posted by Dougal
    And you contradicted yourself in that one sentance.

    But no flaming and back to the topic.
    Howso?

    Anyway, I invite you to read chapter 10.2.8 of the C++ programming language for a better description of classes vs structs. Alternatively 9.0.4 in the C++ standard if you like wading through legalese.

    A quick snippet from tc++pl:
    Quote Originally Posted by Bjarne Stroustrup
    By definition, a struct is a class in which members are by default public; that is,

    struct s { . . .

    is simply shorthand for

    class s { public: . . .

  15. #15
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    No real need to use structs then, you can achieve the same from classes.

    Do remember though, just because a programmer wrote a book on programming, it doesn't mean he/she is a good programmer

  16. #16
    Registered+
    Join Date
    Aug 2005
    Location
    London
    Posts
    44
    Thanks
    0
    Thanked
    0 times in 0 posts
    Quote Originally Posted by KeZZeR
    No real need to use structs then, you can achieve the same from classes.

    Do remember though, just because a programmer wrote a book on programming, it doesn't mean he/she is a good programmer

    Very true, but I think we can make an exception for Mr. Stroustrup here ;-)

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •