encapsulation

How can I expose iterators without exposing the container used?

萝らか妹 提交于 2019-11-28 20:04:45
问题 I have been using C# for a while now, and going back to C++ is a headache. I am trying to get some of my practices from C# with me to C++, but I am finding some resistance and I would be glad to accept your help. I would like to expose an iterator for a class like this: template <class T> class MyContainer { public: // Here is the problem: // typedef for MyIterator without exposing std::vector publicly? MyIterator Begin() { return mHiddenContainerImpl.begin(); } MyIterator End() { return

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

大城市里の小女人 提交于 2019-11-28 18:09:03
In Python, I have the following example class : class Foo: self._attr = 0 @property def attr(self): return self._attr @attr.setter def attr(self, value): self._attr = value @attr.deleter def attr(self): del self._attr As you can see, I have a simple "private" attribute "_attr" and a property to access it. There is a lot of codes to declare a simple private attribute and I think that it's not respecting the "KISS" philosophy to declare all attributes like that. So, why not declare all my attributes as public attributes if I don't need a particular getter/setter/deleter ? My answer will be :

Encapsulation vs Information hiding

旧城冷巷雨未停 提交于 2019-11-28 17:57:57
What exactly are the differences between Ecapsulation and Information Hiding? Well i know that making fields private and then making setter and getter of the fields is ecapsulation.However does encapsulation mean just this? suppose i have a class as described below. public Class IsThisEncapsulation { public int age; public void setAge(int age) { this.age=age; } public int getAge() { return age; } } Now is the class IsThisEncapsulation an example of Encapsulation? Now would making the field 'age' in the above class private achieve informaton hiding? Could you please give me clear examples that

What's the difference between abstraction and encapsulation?

送分小仙女□ 提交于 2019-11-28 15:22:38
问题 In interviews I have been asked to explain the difference between abstraction and encapsulation. My answer has been along the lines of Abstraction allows us to represent complex real world in simplest manner. It is the process of identifying the relevant qualities and behaviors an object should possess; in other words, to represent the necessary feature without representing the background details. Encapsulation is a process of hiding all the internal details of an object from the outside real

encapsulation in javascript module pattern

◇◆丶佛笑我妖孽 提交于 2019-11-28 13:46:39
I was reading this link http://addyosmani.com/largescalejavascript/#modpattern And saw the following example. var basketModule = (function() { var basket = []; //private return { //exposed to public addItem: function(values) { basket.push(values); }, getItemCount: function() { return basket.length; }, getTotal: function(){ var q = this.getItemCount(),p=0; while(q--){ p+= basket[q].price; } return p; } } }()); basketModule.addItem({item:'bread',price:0.5}); basketModule.addItem({item:'butter',price:0.3}); console.log(basketModule.getItemCount()); console.log(basketModule.getTotal()); It stats

Package and visibility

亡梦爱人 提交于 2019-11-28 12:37:10
I'm making an SDK and I'm trying to separate classes to different packages, those classes use some other shared classes. The issue is if I made the shared classes public everyone will be able to see them, not only my classes. What's the right way to make them only accessible by my application? Example : Package a MyClass1 Package b MyClass2 Package c public MySharedClass Because c is public MySharedClass will be able to access it, but the issue is that it will also will be visible to the world, how could I prevent that? Create a package that is documented as an internal package, not to be used

Can I write validation logic in setter methods?

感情迁移 提交于 2019-11-28 09:58:36
Are setter methods only used to set the value of attributes as it is passed as argument? Can we write some validation logic before assigning the value to the attributes? Yes, validation logic is definitely acceptable. It should be noted though that if you have extensive validation you might want to extract this to a specific validator service. But for simple validations you can safely do this. The entire idea behind using getters & setters is so nobody will have direct access to your fields. If you just wanted to set/get the value, you can make them public . Instead, we use setters to validate

Android - how to add @hide annotation in my project

依然范特西╮ 提交于 2019-11-28 09:29:22
I'm developing SDK, and I would like to use @hide annotation for methods/classes which I don't want to be visible for the user who uses my SDK. (same as in Activity implementation - line 3898 ) I tried just to add @hide annotation but I saw that nothing happens. What should I need to do in order to use @hide annotation/or any other similar solution for encapsulating my internal SDK's classes/methods. In Android SDK, the @hide annotation is only used when building the stub version of android.jar . At compile-time symbols are imported from this stub jar and only non-hidden symbols are available.

What good are public variables then?

倖福魔咒の 提交于 2019-11-28 08:54:01
I'm a total newbie with tons of ?'s in my mind and a lot to experience with C++ yet! There's been something which I find really confusing and it's the use of public variables, I've seen tons of code like this: class Foo { private: int m_somePrivateVar; public: void setThatPrivateVar (int const & new_val) { m_somePrivateVar = new_val; } int getThatPrivateVar (void) const { return m_somePrivateVar; } }; Why would anyone hide that variable and implement accessors and mutators when there's nothing done in them more than assigning the new value just as it got received (no range checking etc.) or

C# accessing protected member in derived class [duplicate]

梦想的初衷 提交于 2019-11-28 07:12:41
问题 This question already has answers here : Why can't I access C# protected members except like this? (7 answers) Closed 6 years ago . I wrote the following code: public class A { protected string Howdy = "Howdy!"; } public class B : A { public void CallHowdy() { A a = new A(); Console.WriteLine(a.Howdy); } } Now, in VS2010 it results in the following compilation error: Cannot access protected member 'A.a' via a qualifier of type 'A'; the qualifier must be of type 'B' (or derived from it). This