简体   繁体   中英

Please explain how this is implemented

What I've seen and been taught about encapsulation is that we can have data members as private and member functions are public.

But C++ Primer defines encapsulation as:

Separation of implementation from interface; encapsulation hides the implementation details of a type. In C++, encapsulation is enforced by putting the implementation in the private part of a class.

The last line is the confusing part: putting the implementation in the private part of a class . By implementation, he means the function definitions, right? I mean, I've never seen a function declared public (as prototype) and implemented in private.

I'm really confused. Please explain what he's trying to say with a simple example.

The concept being explained is more abstract than you're thinking about it. The "interface" is "what callers expect the object to do". And the implementation is technically how the class actually carries out those operations.

So for example, take a List class. Code that uses a List should only care about its interface: things like addObject() , clear() , getSize() , sort() .

But the implementation of List -- which callers should not care about -- may vary drastically on how to accomplish this. For example the data could be stored as a linked list. Or maybe as a dynamic array. This is "private" implementation details that only the List class needs to care about. There will indeed probably be a private: method called reallocate() or so. Callers should never use this; it's an implementation detail -- not part of the public interface. Callers should not care how the List allocates its storage.

Similarly, a sort() method implies it will sort objects in the list. That's the interface. But the implementation may use any number of algorithms to perform the sort. The implementation is a private detail - and may be done in private methods. In any case it's hidden from callers.

template<typename T>
class List
{
public:
    // public members are part of the interface: the externally-visible behavior "contract" your object guarantees.
    void addObject(const T& obj)
    {
        // call private methods to help carry out implementation.
        ensureCapacity(this->sizeAllocated + 1);
        this->sizeAdvertized += 1;
        // ... and copy the object into the right position in this->data...
    }

    size_t getSize() const
    {
        return this->sizeAdvertized;
    }

    // ... other members like clear(), sort()...

private:
    T* data;
    size_t sizeAllocated;
    size_t sizeAdvertized;

    // this needs to be private because it's not part of the interface. Callers
    // should never need to call this.
    void ensureCapacity(size_t requestedCapacity)
    {
        if(sizeAllocated >= requestedCapacity)
            return;// already have enough storage available.

        // ... reallocate this->data, copy existing items to the new array...

        this->sizeAllocated = requestedCapacity;
    }
};

And finally to address what you said:

I mean, I've never seen a function declared public (as prototype) and implemented in private.

"Private" again is more abstract than you're thinking. The implementation of a class is basically always "private" in that it's hidden from callers. It doesn't necessarily mean the private: access specifier. It just means that it's not exposed to callers. Implementation of public methods fits this description.

You can indeed have private functions (methods) in a class. Consider:

class X 
{
public:
  // public interface - this never changes and you provide documentation for it
  void do_something();

private:
  // this private interface is likely to change. consumers of this class
  // are prevented from calling these methods unless they are friends
  void first_private_thing();
  void second_private_thing();

};

// definition
void X::do_something()
{
  // private implementation in public method
  first_private_thing();
  second_private_thing();
}

there are further extensions on this theme, for example:

class Base
{
public:
  // public non-virtual interface
  void do_something();

private:
  // calls private virtual implementation
  virtual void do_something_impl() = 0;

};

// definition:
void Base::do_something()
{
  do_something_impl();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM