Delegation can be achieved in C++ by using functors (made even more simple to declare in C++11 with lambdas). Any type can define a call operator member function, so you could say that C++ does have Delegation, just not in an easy syntax, but fixed with lambdas.
To be more precise (see comments), lambda don't act as delegate, functors - that are simply objects you can call like function and therefore move around until you need to call them - act as delegates. Lambdas expressions are just syntactic sugar allowing to define "in place" a functor directly in the code that will use it. Look in the wikipedia page for lambda for an introduction, it's the same as in C# or Python but a bit more powerful because you can put more than one-liners in.
MetaClass is harder but not impossible : look for reflection libraries for similar features. However, it's very rare that such features are really useful in C++ as you can read there the reasons. Most of the time, RTTI is enough.
I agree, those descriptions are really similar. But Abstraction is the idea of consolidating state and functionality into meaningful bundles. Encapsulation is about protecting that bundle so that an outsider can't come in, break the rules, and mess with your state.
- The abstraction has exclusive domain over the state, and encapsulation is how we guarantee that.
Another side of encapsulation is that the implementation may be more complicated than the abstraction. So you might have a method (pseudocode):
List<Thing>
getRelatedThings()
but internally, the list of Things isn't represented as a List at all. Maybe it's a complicated mess of data structures, from which you abstract a List.
Or maybe it is just a list of Things, but you want to be able to change your mind later. The abstraction stays the same, but encapsulation protects the implementation.
- So another difference would be: abstraction strives to simplify, but encapsulation hides complication.
EDIT: one more thing... Encapsulation is an important aspect of decoupling. So let's say that I have two classes that are really similar. Without encapsulation, those abstractions might start to overlap and become more and more inter-twined over time. Encapsulation says, no, this abstraction is distinct from that abstraction.
If there is a common abstraction, factor it out & abstract a base class.
(although now you have two problems ;) ) (...sorry, thats a "favor composition over inheritance" joke. If that doesn't make sense, you'll get there.)
- encapsulation keeps the boundaries of an abstraction distinct
Best Answer
You can write code that looks like this:
or you can write code that looks like this:
If what varies is encapsulated then you don't have to worry about it. You just worry about what you need and whatever you're using figures out how to do what you really need based on what varied.
Encapsulate what varies and you don't have to spread code around that cares about what varied. You just set pet to be a certain type that knows how to speak as that type and after that you can forget which type and just treat it like a pet. You don't have to ask which type.
You might think type is encapsulated because a getter is required to access it. I don't. Getter's don't really encapsulate. They just tattle when someone breaks your encapsulation. They're a nice decorator like aspect oriented hook that is most often used as debugging code. No matter how you slice it, you're still exposing type.
You might look at this example and think I'm conflating polymorphism and encapsulation. I'm not. I'm conflating "what varies" and "details".
The fact that your pet is a dog is a detail. One that might vary for you. One that might not. But certainly one that might vary from person to person. Unless we believe this software will only ever be used by dog lovers it's smart to treat dog as a detail and encapsulate it. That way some parts of the system are blissfully unaware of dog and wont be impacted when we merge with "parrots are us".
Decouple, separate, and hide details from the rest of the code. Don't let knowledge of details spread through your system and you'll be following "encapsulate what varies" just fine.