views:

112

answers:

2

I have a task to create class Encapsulation, with fields in available encapsulation sections. Then I must create an application showing all allowed and forbidden methods of fields access.

What are the encapsulations sections in c++ ? And what methods apart from object.field or *object->field are there anyway ?

+1  A: 

The question is a little unclear but the C++ encapsulation options are public, protected and private. I assume that access methods refer not to the . and -> operators but from where the encapsulated fields can be accessed (public anywhere, protected from base and derived functions, private from base class functions only - unless friend needs to be covered as well).

identitycrisisuk
+1  A: 

Here is a trivial example of C++ encapsulation:

 class Foo{
 public:
     int getBar() const { return m_Bar; }
     void setBar(Bar _value){ m_Bar = _value; }
 private:
     Bar m_Bar;
 };

You see, nothing outside of the class can see a private field. Hence, the only way to access or modify the "Bar" variable is with the get/set methods.

wheaties