Thought For The Day

“A creative artist works on his next composition because he was not satisfied with his last one.”
  Dmitri Shostakovich

 

 

Object-Oriented Programming: Information Hiding and Access Control

In some of the previous articles we mentioned the keywords private and public, and we said that they are called access modifiers, used to control who can access which parts of the code.  Now we will discuss them in greater detail, but first, let's make a short introduction that will help us understand the importance of access control.

One of the concepts on which object-oriented programming was founded is called information hiding.  Information hiding is the principal of separating the functional parts of the code from the user interface, so that the very core of the program is not changed when some design modifications are made.  This way, certain restrictions are introduced for the client: the programmer can (and most likely will) make some parts of the code unreachable for the user, in order to prevent accidental corruption.

Another term related to information hiding is encapsulation.  (There have been debates over the differences between the concepts of information hiding and encapsulation: we will consider that encapsulation is one of the ways of achieving information hiding.)  Simply speaking, encapsulation means "packing" of data and functions into a single module.  In C++, Java, C# and other object-oriented languages, this is achieved through classes.

Encapsulation can be used to hide data members and some methods.  Typically, only the object's own methods can directly inspect or manipulate its fields.

Now we will see how the keywords can be used to specify which code segments the user can access, and which not.
A C++ class can have multiple sections labeled public, protected or private.  Each section remains in effect until another section label is seen.

Note: The default access for members is private.

 

First we will compare the public and private keywords.

A public member is accessible from anywhere outside the class but within a program.  You can set and get the value of public variables directly.

A private member variable or function cannot be accessed, or even viewed from outside the class.  Only the class and friend functions can access private members (we will talk about the friend keyword a bit later).

To illustrate this, we will use the same example that we used in one of the previous articles.

class City
{
public:
    string name;
    int numberOfPpl;
    bool isCapital;
    string country;
};
        
Now, we can write something like:
City city1;
city1.name = "Athens";
since the modifier for the name member was public.

 

But this is not the usual way of implementing a class.  Following the rule of information hiding, the data members are usually declared private, so that they can't be accessed directly, like in the previous example.
However, there should be a way for the user to modify and obtain those values, and this is achieved through functions usually called getters and setters.

For example:

class City
{
private:
    string name;
    int numberOfPpl;
    bool isCapital;
    string country;

public:   //methods (constructors, too!) are usually declared public 
    string getName() //obtaining the value of name member
    {
        return name;
    }
    void setName(string n) //a way to modify the name member 
    {
        name = s;
    }
/*getters and setters for other data members*/
};

int main()
{
    City city1;
    city1.name = "Barselona"; //error - name is private
    city1.setName("Barselona"); //correct way
}

 

The third access modifier we mentioned was protected.  The level of protection that protected offers is the same as the private modifier, with the difference that the members declared protected can also be accessed from the child classes (classes which inherit our class).  We will see an example of using this keyword when we talk about inheritance.  For now, it's just important to understand the private/public concept and why it matters.

Coping During Times of Transition

It’s not uncommon for people to feel anxious during times of transition.  In fact, research shows that even positive transitions—like vacation, marriage, and outstanding personal achievement—can impact our stress levels.  So, how do you cope with a major change?  Below we identify some of the best ways to prepare yourself for your new life, after a significant transition:

  • Exercise.  Exercise is an excellent stress reliever.  It produces endorphins which improve your mood, keeps you focused on a specific task (i.e. a tennis game rather than your problems), improves sleep, and reduces anxiety and depression.

  • Realize that a Mix of Emotions is Normal.  Oftentimes, a major transition can bring up many different feelings.  For instance, if you recently moved across the country, you may feel sad about the friends and family you left behind, worried about finances, excited about your new environment, etc.  According to Psychology Today, feelings of loss and anticipation are commonplace during a transition—avoid the tendency to view sad feelings as evidence that you have made a mistake.

  • Talk It Out.  It’s unhealthy to bottle up your negative emotions.  Instead, talk to a trusted friend or counselor if you are having difficulties dealing with your transition.  Frequently, they can offer non-judgmental compassion and support that will help you to feel a sense of relief.

  • Give Yourself Time to Adjust.  After experiencing a transition, it can be tempting to view the past through rose-colored glasses.  However, if we remember the past with honesty, we can usually admit that we didn’t leave behind a perfect situation.  Keep this in mind when you are tempted to yearn for the past and remember that things will get better.  Periods of transition often seem overwhelming at first but given time, we adjust to them and can see the benefits that we derived from the experience. 

Is Your Company a Great Place to Work?

If you’re a company leader, you already know the importance of attracting and retaining great employees.  Yet while most people in management would agree that this is crucial to the success of an organization, there are differing opinions on how to foster a supportive and synergistic work environment.  Do great companies require perks like unlimited vacation days, free lunches, or gym memberships?  While these types of things are enjoyable, we’d argue that the following items are what really determine a great place to work:

  • Continuous Learning – Great companies invest in their employees.  This might mean bringing in external experts to conduct workshops, offering free college tuition, holding classes that further employees’ skillsets, etc.  Essentially, great companies foster and encourage growth.

  • Employee Appreciation – One of the main reasons that employees change jobs is because they don’t feel appreciated at their current place of employment.  At a great company, employees are recognized for the hard work that they do—this might mean a letter of thanks for a job well-done, an “Employee of the Month” award, or a coveted parking spot.  The point is that the appreciation doesn’t have to be expensive, it just needs to occur.

  • Regular Performance Appraisals – Some companies will go years without conducting performance reviews or giving salary adjustments.  When times are tough, it may not always be possible to give annual raises.  However, this doesn’t mean that employees shouldn’t be kept informed about how well they are meeting expectations.  Ongoing feedback is not only useful to employees, but to the company as a whole.

  • Supportive Environment – When something goes wrong, great companies use it as a learning opportunity, rather than seek to blame and criticize.  Good companies realize that risk is inherent in the business world and they don’t aim to punish those who tried and failed.

Thought For The Day

“In summer, the song sings itself.”
William Carlos Williams