Why encapsulation is an important feature of OOP languages? [closed]

Encapsulation helps in isolating implementation details from the behavior exposed to clients of a class (other classes/functions that are using this class), and gives you more control over coupling in your code. Consider this example, similar to the one in Robert Martin’s book Clean Code: public class Car { //… public float GetFuelPercentage() { /* … Read more

“public” or “private” attribute in Python ? What is the best way?

Typically, Python code strives to adhere to the Uniform Access Principle. Specifically, the accepted approach is: Expose your instance variables directly, allowing, for instance, foo.x = 0, not foo.set_x(0) If you need to wrap the accesses inside methods, for whatever reason, use @property, which preserves the access semantics. That is, foo.x = 0 now invokes … Read more

IEnumerable vs IReadonlyCollection vs ReadonlyCollection for exposing a list member

One important aspect seems to be missing from the answers so far: When an IEnumerable<T> is returned to the caller, they must consider the possibility that the returned object is a “lazy stream”, e.g. a collection built with “yield return”. That is, the performance penalty for producing the elements of the IEnumerable<T> may have to … Read more

How to access private data members outside the class without making “friend”s? [duplicate]

Here’s a way, not recommended though class Weak { private: string name; public: void setName(const string& name) { this->name = name; } string getName()const { return this->name; } }; struct Hacker { string name; }; int main(int argc, char** argv) { Weak w; w.setName(“Jon”); cout << w.getName() << endl; Hacker *hackit = reinterpret_cast<Hacker *>(&w); hackit->name … Read more

Understanding the difference between __getattr__ and __getattribute__

Some basics first. With objects, you need to deal with their attributes. Ordinarily, we do instance.attribute. Sometimes we need more control (when we do not know the name of the attribute in advance). For example, instance.attribute would become getattr(instance, attribute_name). Using this model, we can get the attribute by supplying the attribute_name as a string. … Read more

accessing a protected member of a base class in another subclass

When foo receives a FooBase reference, the compiler doesn’t know whether the argument is a descendant of Foo, so it has to assume it’s not. Foo has access to inherited protected members of other Foo objects, not all other sibling classes. Consider this code: class FooSibling: public FooBase { }; FooSibling sib; Foo f; f.foo(sib); … Read more

Any reason to use auto-implemented properties over manual implemented properties?

It doesn’t grant you anything extra beyond being concise. If you prefer the more verbose syntax, then by all means, use that. One advantage to using auto props is that it can potentially save you from making a silly coding mistake such as accidentally assigning the wrong private variable to a property. Trust me, I’ve … Read more

tech