encapsulation

When should you use 'friend' in C++?

I have been reading through the C++ FAQ and was curious about the friend declaration. I personally have never used it, however I am interested in exploring the language. What is a good example of using friend? Edit: Reading the FAQ a bit longer I like the idea of the << >> operator overloading and adding as a friend of those classes....

Abstraction VS Information Hiding VS Encapsulation

Can you tell me what is difference between ABSTRACTION and INFORMATION HIDING in software development? I am confused abstraction hides detail implementation and information hiding abstracts whole details of something. updated: I found good answer for these three concepts. From here: http://www.itmweb.com/essay550.htm Abstraction: ...

Python descriptor protocol analog in other languages?

Is there something like the Python descriptor protocol implemented in other languages? It seems like a nice way to increase modularity/encapsulation without bloating your containing class' implementation, but I've never heard of a similar thing in any other languages. Is it likely absent from other languages because of the lookup overhea...

Why are Python's 'private' methods not actually private?

Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like so: *__myPrivateMethod()*. How, then, can one explain this >>> class MyClass: ... def myPublicMethod(self): ... print 'public method' ... def __myPrivateMethod(self): ... ...

Private vs. Public members in practice (how important is encapsulation?)

One of the biggest advantages of object-oriented programming is encapsulation, and one of the "truths" we've (or, at least, I've) been taught is that members should always be made private and made available via accessor and mutator methods, thus ensuring the ability to verify and validate the changes. I'm curious, though, how important ...

Is it a good (correct) way to encapsulate a collection?

class MyContainedClass { }; class MyClass { public: MyContainedClass * getElement() { // ... std::list<MyContainedClass>::iterator it = ... // retrieve somehow return &(*it); } // other methods private: std::list<MyContainedClass> m_contained; }; Though msdn says std::list should not perform relocations of elements...

How can I expose iterators without exposing the container used?

Hello world! 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: /...

Subclassing a class with private members

One of the really nice things about python is the simplicity with which you can name variables that have the same name as the accessor: self.__value = 1 def value(): return self.__value Is there a simple way of providing access to the private members of a class that I wish to subclass? Often I wish to simply work with the raw dat...

Why not use 'protected' or 'private' in PHP?

I've been working with the Joomla framework and I have noticed that they use a convention to designate private or protected methods (they put an underscore "_" in front of the method name), but they do not explicitly declare any methods public, private, or protected. Why is this? Does it have to do with portability? Are the public, pr...

Class design with vector as a private/public member ?

what is the best way to put a container class or a some other class inside a class as private or a public member? Requirements: 1.Vector< someclass> inside my class 2.Add and count of vector is needed interface ...

When would you use the "protected internal" access modifier?

As you may already know, the .NET Framework's protected internal access modifier works in a strange way: It doesn't mean the class is protected AND internal, it says the class is protected OR internal; that is, the modified class or member can be accessed from whitin the same assembly as well as from the same hierarchy. So, knowing this...

How do you get people to value abstraction and flexibility over "just getting it done"?

I sometimes have difficulties with other people who wish to solve a problem when they wish to skip the official interfaces and access underlying implementation details directly. They argue that doing so will allow them to solve the problem more quickly. I argue that doing so will cause our architecture to become more tightly coupled an...

Encapsulating Java Preferences API

I used to have a custom preferences class for my applications. For my next hobby project i wanted to switch to the Preferences API. But the put and get functions require a default value and i do not want to spread default values all over the source files. Even though my project is small i can not imagine changing default values all over ...

C#: Partial Classes & Web Services: Seperating form and functionality

Hi all, I am dabbling in the world of web services and I've been making a simple web service which mimics mathematical operations. Firstly it was simple, passing in two integers and then a binary operator would be applied to these (plus, minus etc) depending on the method called. Then I decided to make things a little more complex and ...

When do you stop encapsulating?

I have some event handler on a boundary class that manages a persistence mechanism for a given generic transaction: void MyBoundaryClass::MyEventHandler(...) { //retrieve stuff from the UI //... //declare and initialize trasaction to persist SimpleTransaction myTransaction(.../*pass down stuff*/); //do some other checks //.....

Encapsulation VS Inheritance - How to use a protected function?

In OOP languages like C# or VB.NET, if I make the properties or functions in a super class protected I can't access then in my Form, They can only be access in my class that inherits from that super class. To access those properties or functions I need to make them public which defeats encapsulation on re-write them in my class which de...

Encapsulation Principle

There's some object-oriented engineering principle that states something along the lines of "a class should only know about the contracts of the classes that it takes as arguments, or any internal ones it uses." The counter-example, in C++, is: Foo::bar( Baz* baz) { baz()->blargh()->pants()->soil(); // this is bad, Foo knows about b...

What are the different types of encapsulation?

This question came from the worst interview questions thread here So, what are the different types of encapsulation? Am I right in thinking this basically refers to central OO concepts such as Abstraction, Polymorphism and Inheritance? My understanding of encapsulation is that it is a method of hiding data / functionality, but I never...

Do any other developers get yelled at for making every thing public?

I am sick and tired of getting yelled at when I make all my variables in a Java class public, for the sake of not needing to write five times more code for getters and setters. Do others share this problem? ...

Creating method in implementation without defining in header

How can I create a method in the @implementation of a class without defining it in the @interface? For example, I have a constructor that does some initialisation and then reads data from a file. I want to factor out the file-reading code into a separate method that I then call from within the constructor. I don't want to define this ...